Pergunta

I have this markup:

<input type="checkbox" value="Y" name="addNextGroup[1][]">
<input type="checkbox" value="Y" name="addNextGroup[2][]">
<input type="checkbox" value="Y" name="addNextGroup[3][]">
<input type="checkbox" value="Y" name="addNextGroup[4][]">
<input type="checkbox" value="Y" name="addNextGroup[5][]">

I need to create an onclick event listener so I can launch a function in jquery. I am trying to piece it together like this, but I feel it is wrong:

for(i=1; i<=5; i++) {
  var myEl + i = document.getElementsByTagName('addNextGroup\\[' + i + '\\]\\[\\]');
}
Foi útil?

Solução

Why not simply:

$('input[name^="addNextGroup"]').click(function(){...})

Outras dicas

The simplest way I can think to do this is to add to the mark up:

<input type="checkbox" value="Y" name="addNextGroup[1][]" onclick='function();'>

If you must use jquery, first add a class to all the input elements that you would like to trigger the function onclick with like so:

<input type="checkbox" value="Y" name="addNextGroup[1][]" class='listener'>

Then add jquery code like this to the JS file.

$(".listener").click(function(){
    ENTER FUNCTION HERE
});

EDIT:

You may have to have that Jquery line above inside the following jquery (maybe not I am rusty):

$(document).ready(function() {

});
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top