# Simple check\_box

**URL:** <https://discuss.rubyonrails.org/t/simple-check-box/59047>\
**Category:** rubyonrails-talk\
**Created:** [May 19, 2011, 12:15pm UTC](https://discuss.rubyonrails.org/t/simple-check-box/59047 "2011-05-19T12:15:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [May 19, 2011, 12:15pm UTC](https://discuss.rubyonrails.org/t/simple-check-box/59047/1 "2011-05-19T12:15:53Z")

</div>

Hello,

I'm trying create a simple check\_box that if checked include a hidden\_field, but I haven't success.

Something like this: &nbsp;&nbsp;&nbsp;&nbsp;\<p class="field"\>\</p\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= check\_box\_tag(:patrono, value = "1", checked = false, options = {}) %\> &nbsp;&nbsp;&nbsp;&nbsp;\<%= f.label "Patrono" %\> &nbsp;&nbsp;&nbsp;&nbsp;\<% if :patrono.checked == true %\> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\<%= f.hidden\_field "tipo\_componente\_ids][", { :id =\> "tipo\_componente\_ids", :value =\> @patrono.id } %\> &nbsp;&nbsp;&nbsp;&nbsp;\<% end %\> &nbsp;&nbsp;&nbsp;&nbsp;\<p/\>

Thanks for help!

---

<div class="post-metadata">

**Author:** ![11155](https://avatars.discourse-cdn.com/v4/letter/1/d26b3c/32.png) [@11155](https://discuss.rubyonrails.org/u/11155)\
**Post date:** [May 19, 2011, 1:21pm UTC](https://discuss.rubyonrails.org/t/simple-check-box/59047/2 "2011-05-19T13:21:35Z")

</div>

Eric,

You have to write javascript function within the check\_box\_tag in order to verify whether the checkbox is checked or not and then enable the hidden field if it is checked.

Saravanan K

> **[Sarav's Weblog](https://saravani.wordpress.com/)**
>
> Technical Articles for RoR Developers

---

<div class="post-metadata">

**Author:** ![Tom\_Meinlschmidt](https://avatars.discourse-cdn.com/v4/letter/t/c89c15/32.png) [@Tom\_Meinlschmidt](https://discuss.rubyonrails.org/u/Tom_Meinlschmidt)\
**Post date:** [May 19, 2011, 1:41pm UTC](https://discuss.rubyonrails.org/t/simple-check-box/59047/3 "2011-05-19T13:41:14Z")

</div>

hi,

use jQuery and then

= f.hidden\_field\_tag "hidden\_field", "value", { :id =\> "hidden\_field"} = f.check\_box\_tag "name", 1,false, {:id =\> "my\_checkbox"}

jQuery('#my\_checkbox').bind('click', function() { &nbsp;&nbsp;jQuery('#hidden\_field').attr('disabled', !jQuery(this).attr('checked')); }

when field is disabled, it's not taken into account when form is sent

PS: if not using jQuery, it's simple to do with DOM too, just add :onClick='disableField()' to the checkbox and function disableField() { &nbsp;&nbsp;document.getElementById('hidden\_field').disabled = !document.getElementById('my\_checkbox').checked; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false; }

tom
