2
I’m using a loop to generate elements dynamically:
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>";
}
Can I capture the index of the clicked button ? For example:
Problem:
If I click the second button, I return 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>
<link href="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>
<table id="table" class="table table-bordered">
<tbody id="tbody-content"></tbody>
</table>
In the jsFiddle
there is a way to get the index without being by value? I tried to pass i to function buttons[i]. addeventlistener('click', acaoBotao(i), false); , but it did not work
– Denali