Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
47
rated 0 times [  49] [ 2]  / answers: 1 / hits: 18840  / 12 Years ago, thu, december 13, 2012, 12:00:00

I want to remove the selected attribute from an option element when I uncheck this. I dont want to make value false or anything... I just want to delete this property. My code is like



$(function() {
$(#deployselect).multiselect({
click: function( event, ui ) {
if(ui.checked==false){
$(#deployselect option[value='+ui.value+']).removeAttr(selected);
}
}
});
});


My generated HTML is like this



<option value=68 selected>A1</option>
<option value=49 selected>A2</option>
<option value=69 selected>A3</option>


so even i make attr or prop false it is not working. :(


More From » jquery

 Answers
45

If you want to de-select it, change this line:



$(#deployselect option[value='+ui.value+']).removeAttr(selected);


to



$(#deployselect option[value='+ui.value+']).prop(selected, false);


Live Example | Source



(Assuming jQuery 1.6 or later; for earlier versions, change prop to attr.)



Once the HTML has been parsed into the DOM, the option element has a property called selected which gets its initial value from the selected attribute in the HTML (defaulting to false). So once you're dealing with DOM elements, you're dealing with a boolean property, not an attribute anymore.


[#81447] Wednesday, December 12, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
dylondaytond

Total Points: 92
Total Questions: 88
Total Answers: 96

Location: China
Member since Fri, Jan 15, 2021
3 Years ago
dylondaytond questions
Tue, Jun 22, 21, 00:00, 3 Years ago
Thu, May 7, 20, 00:00, 4 Years ago
;