# Observe\_field, how to use input to toggle checkbox?

**URL:** <https://www.ruby-forum.com/t/observe-field-how-to-use-input-to-toggle-checkbox/186029>\
**Category:** Rails\
**Created:** [March 30, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/observe-field-how-to-use-input-to-toggle-checkbox/186029 "2010-03-30T17:19:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 30, 2010, 5:19pm UTC](https://www.ruby-forum.com/t/observe-field-how-to-use-input-to-toggle-checkbox/186029/1 "2010-03-30T17:19:41Z")

</div>

This may seem like a bit of a silly question, but here’s the reason:

I have an app where a large number of options may need to be selected.  
Some are simply on/off with a checkbox but others have an additional  
text input field associated with them. This is OK if users read the  
instructions or they only select an on/off option, but sometimes for the  
options with additional text they simply type into the input field and  
neglect to tick the box as well. One way around this would be to  
re-write the code to remove the requirement for checkboxes on  
everything, but before doing that I would like to try using  
observe\_field to tick the box if they start typing text in the field. Is  
that possible?

So far I have something like this:

\<%= observe\_field “text\_input[#{parameter}]”, :update =\>  
“check\_box[#{parameter}]”, :parameters =\> 1, :frequency =\> 0.5 -%\>

This does not work. Is anyone able to suggest how this might be fixed?  
Thanks.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 30, 2010, 5:54pm UTC](https://www.ruby-forum.com/t/observe-field-how-to-use-input-to-toggle-checkbox/186029/2 "2010-03-30T17:54:50Z")

</div>

You could use the JS onblur to enable the check box.  
\<%= text\_field\_tag “text”,“field” :onblur =\>  
‘check\_text\_field\_for\_text(this)’ %\>  
then use a js function to check the text box for text and set the  
checkbox accordingly.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [March 31, 2010, 2:24pm UTC](https://www.ruby-forum.com/t/observe-field-how-to-use-input-to-toggle-checkbox/186029/3 "2010-03-31T14:24:46Z")

</div>

Chris H. wrote:

> then use a js function to check the text box for text and set the  
> checkbox accordingly.

Thanks. So, I now have this:

\<%= text\_field\_tag “text\_value[#{var}]”,"", :onblur =\>  
“check\_text\_field\_for\_text(#{var})” -%\>

…and:

Still no luck, though. Presumably I am not referencing the name of the  
checkbox correctly, but I am not sure of the nature of the error.

---

<div class="post-metadata">

**Author:** ![milo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@milo](https://www.ruby-forum.com/u/milo)\
**Post date:** [April 1, 2010, 5:09pm UTC](https://www.ruby-forum.com/t/observe-field-how-to-use-input-to-toggle-checkbox/186029/4 "2010-04-01T17:09:29Z")

</div>

I have finally had some success, and it turns out that it was as simple  
as this:

\<%= observe\_field(“text\_input\_#{parameter}”,:function =\>  
“check\_box\_#{parameter}.checked=true”) -%\>
