What is the correct way to get a control that was rendered by ASP.NET with jQuery?
Example: I have a checkbox that was generated like this:
<input id="ctl00_Content_chkOk" type="checkbox" name="ctl00$Content$chkOk" />
If I want to select it to get if it's checked, I tried this:
$('form input[name=chkOk]').attr("checked")
and
$('chkOk').attr("checked")
But it didn't work, I had to do it like this
$('form input[name=ctl00$Content$chkOk]').attr("checked")
And I guess this would've worked too:
$('ctl00$Content$chkOk').attr("checked")
Is this the correct way to do it? Is there another selector I can use?
You can use the server side control ClientID property:
var isChecked = $('#<%=YourCheckBox.ClientID%>').attr("checked");
or you could use the "ends with" selector: attribute$=value
var isChecked = $('form input[name$=chkOk]').attr("checked");
I always used this notation
$('#ctl00_Content_chkOk:checked').length; // will evaluate as true when checked
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With