Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
133
rated 0 times [  138] [ 5]  / answers: 1 / hits: 106140  / 15 Years ago, tue, january 5, 2010, 12:00:00

I have following HTML with two elements having the same name



<input type=hidden name= chk0 value=>
<input type=checkbox name=chk0 value=true disabled>


Through JQuery, I want to set the enable the checkbox. So, I am using something like this:



$('#chk0').attr(disabled,false);


But this doesn't work. I assume JQuery is getting confused with two elements having the same identical name. Unfortunatel, I cannot avoid using two different names because, when the form is posted, I want all the checkboxes to get posted (not just the ones that are checked). Hence I have to use hidden element with the same name.. So, back to the question, how can I enable the checkbox through JQuery in the above scenario? Is there a type parameter for attr which distingues hidden from checkbox?



Thanks


More From » jquery

 Answers
28

Some things before the actual code..



the hash (#) you use as the selector is for IDs and not for names of elements.
also the disabled attribute is not a true false scenario .. if it has disabled attribute it means that it is true .. you need to remove the attribute and not set it to false.
Also there are the form selectors that identify specific types of items in a form ..



so the code would be



$(input:checkbox[name='chk0']).removeAttr('disabled');





Bringing the answer up-to-date



You should use the .prop() method (added since v1.6)



$(input:checkbox[name='chk0']).prop('disabled', false); // to enable the checkbox


and



$(input:checkbox[name='chk0']).prop('disabled', true); // to disable the checkbox

[#97915] Friday, January 1, 2010, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
marisela

Total Points: 103
Total Questions: 105
Total Answers: 102

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
;