views:

60

answers:

1

I'm using a fields_for helper in rails with some text boxes. How can I get the ID of these text boxes to use in some Javascript? I would like to be able to manipulate the status of the other checked boxes by clicking on certain boxes. Thanks :)

Stuff like:

  <% fields_for "[id][]", app  do |fields| %>
       <%= fields.check_box :featured %>
...

and then do something with scriptaculous or an onclick to deal with what happens to other generated check boxes...thanks

A: 

Predicting ids gets too messy on dynamic forms for reliable javascript.

Instead you can use the prototype helpers and class names to achieve similar functionality.

Make this javascript available to your page:

var myrules = {
  '.highlightFeatured': function (e){ 
  clicked_on_box = Event.element(e)
  container = clicked_on_box.up('form')
  box_to_modify = container.down('.featured')
  new Effect.Highlight(box_to_modify)
  };
};

Event.observe(window, 'load', function(){
  $('fields').delegate('click', myrules);
});

And define your view along these lines. Now when the bogus check box is clicked featured will be highlighted.

<% fields_for "[id][]", app  do |fields| %>
  <div id="fields">
   <%= fields.check_box :featured, :class => "featured" %>
   <%= fields.check_box :bogus, :class => "highlightFeatured" %>
   ...
  </div>
<% end %>
EmFi