3

I created this code:

$("input[name*='Gridview1$ctl02']").each(function () {
   if(this.type == 'checkbox'){
       if(this.checked == true){
           alert("test")
        }
       else
        {
           alert("test2")
        }
    }
})

Its good when I write this $("input[name*='Gridview1$ctl02']") but I need array of ct101,ct102,ct103

enter image description here

I need Something like this:

$("input[name*='Gridview1']").find("ct").each ...
Rohit Arora
  • 2,246
  • 2
  • 24
  • 40
Rafał Developer
  • 2,135
  • 9
  • 40
  • 72

2 Answers2

4

You can simple 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 want only text inputs:

$("input[name^='Gridview1'][type='text']").each(function () {if(this.type == 'checkbox'){if(this.checked == true){alert("test")}else{alert("test2")}}})
Alex Char
  • 32,879
  • 9
  • 49
  • 70
3

Something like this should match all the names you require AND those that are checked checkboxes:

$("input[name^='Gridview1$ctl'][type='checkbox']:checked")

See it here (it will alert names of checked checkboxes after you hit 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>
Shomz
  • 37,421
  • 4
  • 57
  • 85