Forum: Ruby on Rails Check a checkbox and disable a field with Javascript

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2008-12-03 20:07
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:
<script type="text/javascript">
function toggle_fruit_price(lqname) {
    $(lqname).disabled = (!$(lqname).disabled);
  }
</script>

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.
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-03 20:11
(Received via mailing list)
On 3 Dec 2008, at 19:07, Joe Peck wrote:

> <script type="text/javascript">
>
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
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2008-12-03 20:19
> 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
<script type="text/javascript">
function toggle_weight_boxes(lqname) {
    $('lqname').disabled = (!$('lqname').disabled);
  }
</script>

and that doesn't work.  I'm sure I'm only a tiny bit off, how else
should I write this?
E52b2a55c7aebe6bd3e04c664aa01b39?d=identicon&s=25 Aldo Brett (mrbrett)
on 2008-12-03 20:35
Joe Peck 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:
> <script type="text/javascript">
> function toggle_fruit_price(lqname) {
>     $(lqname).disabled = (!$(lqname).disabled);
>   }
> </script>
>
> 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()
81b61875e41eaa58887543635d556fca?d=identicon&s=25 Frederick Cheung (Guest)
on 2008-12-03 20:37
(Received via mailing list)
On Dec 3, 7:19 pm, Joe Peck <rails-mailing-l...@andreas-s.net> wrote:
>
> Hmm, well I have also tried
> <script type="text/javascript">
> function toggle_weight_boxes(lqname) {
>     $('lqname').disabled = (!$('lqname').disabled);
>   }
> </script>
>
> 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
2119f016588ba13373484491bd2dd6d1?d=identicon&s=25 Joe Peck (fatcatt316)
on 2008-12-03 21:41
> 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 :)
This topic is locked and can not be replied to.