# Check a checkbox and disable a field with Javascript

**URL:** <https://www.ruby-forum.com/t/check-a-checkbox-and-disable-a-field-with-javascript/154776>\
**Category:** Rails\
**Created:** [December 3, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/check-a-checkbox-and-disable-a-field-with-javascript/154776 "2008-12-03T20:07:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 3, 2008, 8:07pm UTC](https://www.ruby-forum.com/t/check-a-checkbox-and-disable-a-field-with-javascript/154776/1 "2008-12-03T20:07:28Z")

</div>

Good afternoon to you all,

I have an order form where people can check the fruits they want and  
select the price of each fruit that they have checked.

When I click the checkbox now, it does not seem to affect the  
corresponding price dropdown. What am I doing wrong?

Here’s the javascript:

Then, inside my loop of @fruits:  
\<%= fruit.name %\>  
\<%= check\_box\_tag fruit.name, 1, false, :onchange =\>  
“toggle\_fruit\_price(#{fruit.name}\_price);” %\>  
\<%= select\_tag("#{fruit.name}\_price", options\_for\_select((0…100))) %\>

Does anyone see what I’m doing wrong? Any help is appreciated.

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 3, 2008, 8:11pm UTC](https://www.ruby-forum.com/t/check-a-checkbox-and-disable-a-field-with-javascript/154776/2 "2008-12-03T20:11:50Z")

</div>

On 3 Dec 2008, at 19:07, Joe P. wrote:

> 

If you think of the javascript that will be generated it’s just going  
to be

toggle\_fruit\_price(apple\_price);

whereas you want it to be

toggle\_fruit\_price(‘apple\_price’);

Fred

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 3, 2008, 8:35pm UTC](https://www.ruby-forum.com/t/check-a-checkbox-and-disable-a-field-with-javascript/154776/3 "2008-12-03T20:35:45Z")

</div>

Joe P. wrote:

> Good afternoon to you all,
> 
> I have an order form where people can check the fruits they want and  
> select the price of each fruit that they have checked.
> 
> When I click the checkbox now, it does not seem to affect the  
> corresponding price dropdown. What am I doing wrong?
> 
> Here’s the javascript:
> 
> Then, inside my loop of @fruits:  
> \<%= fruit.name %\>  
> \<%= check\_box\_tag fruit.name, 1, false, :onchange =\>  
> “toggle\_fruit\_price(#{fruit.name}\_price);” %\>  
> \<%= select\_tag("#{fruit.name}\_price", options\_for\_select((0…100))) %\>
> 
> Does anyone see what I’m doing wrong? Any help is appreciated.

Maybe you can try somthing like this:

if $(‘lqname’).checked == “1”  
$(‘fruit\_price\_field’).show()

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 3, 2008, 8:19pm UTC](https://www.ruby-forum.com/t/check-a-checkbox-and-disable-a-field-with-javascript/154776/4 "2008-12-03T20:19:25Z")

</div>

> If you think of the javascript that will be generated it’s just going  
> to be
> 
> toggle\_fruit\_price(apple\_price);
> 
> whereas you want it to be
> 
> toggle\_fruit\_price(‘apple\_price’);
> 
> Fred  
> Hmm, well I have also tried

and that doesn’t work. I’m sure I’m only a tiny bit off, how else  
should I write this?

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 3, 2008, 8:37pm UTC](https://www.ruby-forum.com/t/check-a-checkbox-and-disable-a-field-with-javascript/154776/5 "2008-12-03T20:37:43Z")

</div>

On Dec 3, 7:19 pm, Joe P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hmm, well I have also tried
> 
> and that doesn’t work. I’m sure I’m only a tiny bit off, how else  
> should I write this?

That wasn’t the problem - the toggle\_fruit\_price function was fine as  
it was. The problem is with this statement

toggle\_fruit\_price(apple\_price);

Forget that it’s js for a second - that statement isn’t legal if there  
is no variable named apple\_price, which there isn’t

Fred

---

<div class="post-metadata">

**Author:** ![joep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/87869e/32.png) [@joep](https://www.ruby-forum.com/u/joep)\
**Post date:** [December 3, 2008, 9:41pm UTC](https://www.ruby-forum.com/t/check-a-checkbox-and-disable-a-field-with-javascript/154776/6 "2008-12-03T21:41:06Z")

</div>

> That wasn’t the problem - the toggle\_fruit\_price function was fine as  
> it was. The problem is with this statement
> 
> toggle\_fruit\_price(apple\_price);
> 
> Forget that it’s js for a second - that statement isn’t legal if there  
> is no variable named apple\_price, which there isn’t
> 
> Fred

Phew, thanks. Javascript is not my forte, but I’m learning more of it  
now. Thanks for the help 🙂
