Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle Checkboxes on/off

I have the following:

$(document).ready(function() {     $("#select-all-teammembers").click(function() {         $("input[name=recipients\\[\\]]").attr('checked', true);     });                  }); 

I'd like the id="select-all-teammembers" when clicked to toggle between checked and unchecked. Ideas? that aren't dozens of lines of code?

like image 639
AnApprentice Avatar asked Nov 14 '10 10:11

AnApprentice


People also ask

How do you know if toggle is on or off?

settings:id/switch_widget\"]"). getAttribute("Checked"); If the element is disabled then you will see the Result Data as "false" and if it is enabled then you will see the "true." You can use the element identifier: //*[@text="ON"] if there is only one toggle element/switch enabled on the screen.

How do you check checkbox is on or off in jQuery?

click(function() { var checkBoxes = $("input[name=recipients\\[\\]]"); checkBoxes. prop("checked", !


1 Answers

You can write:

$(document).ready(function() {     $("#select-all-teammembers").click(function() {         var checkBoxes = $("input[name=recipients\\[\\]]");         checkBoxes.prop("checked", !checkBoxes.prop("checked"));     });                  }); 

Before jQuery 1.6, when we only had attr() and not prop(), we used to write:

checkBoxes.attr("checked", !checkBoxes.attr("checked")); 

But prop() has better semantics than attr() when applied to "boolean" HTML attributes, so it is usually preferred in this situation.

like image 180
Frédéric Hamidi Avatar answered Sep 29 '22 00:09

Frédéric Hamidi