I understand that jquery will allow you to modify attributes with the .attr() method. There are basically two methods:
$('#element').attr('attribute', 'value') // sets the attribute
var attribute = $('#element').attr('attribute') // gets the attribute
My question is, how do you set a boolean attribute such as ‘checked’ on a checkbox or ‘multiple’ on a select tag?
I’ve tried doing the following without success:
$('#element').attr('attribute', true)
$('#element').attr('attribute', '')
All of these add the attribute but usually like this <tag attribute="attribute">.
Try using
.propto deal withbooleanthat is for supported attributes likeselected/disabled/checkede.t.cfrom jQuery docs, (an example)
elem.checkedreturnstrue(Boolean) Will change with checkbox state$(elem).prop("checked")returnstrue(Boolean) Will change with checkbox stateelem.getAttribute("checked")returns"checked"(String) Initial state of the checkbox; does not change$(elem).attr("checked")(1.6)returns"checked"(String) Initial state of the checkbox; does not change$(elem).attr("checked")(1.6.1+)returns"checked"(String) Will change with checkbox state$(elem).attr("checked")(pre-1.6)returnstrue(Boolean) Changed with checkbox state