Skip to content Skip to sidebar Skip to footer

How Do You Set And Get The Id Of Check_box_tag For Javascript Function?

I have a Ruby on Rails 3 form with check_box_tags. How do you set an ID and then get the value in a javascript function? Here is the check_box_tag

Solution 1:

Per your instructions, you are just checking to see if at least one of two specific checkboxes is checked:

$(document).ready(function() {
  $('#device').change(function(event){
    if ($(event.target).filter("input[value='IP Phone']:checked").length ||
      $(event.target).filter("input[value='IP PBX Systems']:checked").length)
    {
        $('#phonepbx').css('display', 'inline');
    }
    else
    {
        $('#phonepbx').css('display', 'none');
    }
  });
});

Solution 2:

It looks to me like you're trying to trap the checkbox events by adding a change handler to the div. That's not the ideal place to trap it, as you then have to search for what changed. But as RustyToms said, your ID should be 'survey[hardware][]' as set by RoR.

$(document).ready(function() {
  $('#survey[hardware][]').on('click', function(){
    (this.checked && this.val().match(/IP Phone|IP PBX Systems/)) ? $('#phonepbx').hide() : $('#phonepbx').show();
  });
});

Post a Comment for "How Do You Set And Get The Id Of Check_box_tag For Javascript Function?"