Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery and ASP.NET names

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?

like image 729
juan Avatar asked Feb 27 '09 20:02

juan


2 Answers

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");
like image 110
Christian C. Salvadó Avatar answered Sep 26 '22 16:09

Christian C. Salvadó


I always used this notation

$('#ctl00_Content_chkOk:checked').length; // will evaluate as true when checked
like image 25
Jeff Davis Avatar answered Sep 26 '22 16:09

Jeff Davis