Check a checkbox and disable a field with Javascript


#1

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.


#2

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


#3

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()


#4

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?


#5

On Dec 3, 7:19 pm, Joe P. 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


#6

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 :slight_smile: