I'm using a loop to dynamically generate elements:
for(var i =0; i< arrSequencia.length;i++ ){
strInterface += "<tr class='formulario'>"+
"<td class='col-md-1'>"+
" <input type=\"button\" class=\"btn btn-danger btn-info-bloco sembloco\" value=\"Botao "+arrSequencia[i]+"\"/>"+
"</td>"+
"</tr>";
}
How can I capture the index of the clicked button? For example:
Problem:
If I click the second button, I get the index: 1
var acaoBotao = function() {
//PEGAR O INDEX
};
function adicionaOuRemove() {
var botoes = document.getElementsByClassName("btn-info-bloco");
for(var i=0;i<botoes.length;i++){
botoes[i].addEventListener('click', acaoBotao, false);
}
}
Example:
var arrSequencia = [1,2,3,4,5];
var strInterface ="";
$(document).ready(function(){menuQuestoes = $("#table");
for(var i =0; i< arrSequencia.length;i++ ){
strInterface += "<tr class='formulario'>"+
"<td class='col-md-1'>"+
" <input type=\"button\" class=\"btn btn-danger btn-info-bloco sembloco\" value=\"Botao "+arrSequencia[i]+"\"/>"+
"</td>"+
"</tr>";
}
menuQuestoes.find("#tbody-content").append(strInterface);
});
var acaoBotao = function() {
alert("//PEGAR O INDEX");
};
function adicionaOuRemove() {
var botoes = document.getElementsByClassName("btn-info-bloco");
for(var i=0;i<botoes.length;i++){
botoes[i].addEventListener('click', acaoBotao, false);
}
}
adicionaOuRemove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><linkhref="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script><tableid="table" class="table table-bordered">
<tbody id="tbody-content"></tbody>
</table>
No jsFiddle