JQuery - get down with selector name
I created this code:
$("input[name*='Gridview1$ctl02']").each(function () {
if(this.type == 'checkbox'){
if(this.checked == true){
alert("test")
}
else
{
alert("test2")
}
}
})
Ok when I write this $("input[name*='Gridview1$ctl02']")
but I need an arrayct101,ct102,ct103
I need something like this:
$("input[name*='Gridview1']").find("ct").each ...
+3
source to share
2 answers
You can simply create a selector to match any element where their name starts with Gridview1:
$("input[name^='Gridview1']").each(function () {if(this.type == 'checkbox'){if(this.checked == true){alert("test")}else{alert("test2")}}})
Alternative if you only want text inputs:
$("input[name^='Gridview1'][type='text']").each(function () {if(this.type == 'checkbox'){if(this.checked == true){alert("test")}else{alert("test2")}}})
+4
source to share
Something like this should match all the required names and the ones the checkboxes are set to:
$("input[name^='Gridview1$ctl'][type='checkbox']:checked")
See here (it will warn the names of the checked boxes after you click Test):
function test() {
$("input[name^='Gridview1$ctl'][type='checkbox']:checked").each(function() {
alert($(this).attr('name'));
})
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" name="Gridview1$ctl01" />
<input type="text" name="Gridview1$ctl02" />
<input type="checkbox" name="Gridview1$ctl03" />
<button onclick="test()">Test</button>
+3
source to share