1
I am populating a table with data coming from the database, the ids are dynamic using the database id, need to recover these ids with javascript, follow the code as an example.
function selecionaDactes(){
var ids = document.getElementsByClassName('editar');
gravaArray(ids);
}
function gravaArray(dados){
var array_dados = Array;
array_dados = dados;
for(var x = 0; x <= array_dados.length; x++){
if(array_dados[x].checked){
alert(array_dados[x].value);
}else{
alert('não há dados selecionados' + array_dados[x].value);
}
}
}
<table border="1">
<tr>
<td><input class="editar" type="checkbox" id="187" value="187"></td>
<td>Vasco</td>
<td><button type="button" onclick="selecionaDactes()">Editar</button></td>
</tr>
<tr>
<td><input type="checkbox" id="90" value="90"></td>
<td>Flamengo</td>
<td><button type="button" onclick="selecionaDactes()">Editar</button></td>
</tr>
<tr>
<td><input class="editar" type="checkbox" id="341" value="341"></td>
<td>Corinthians</td>
<td><button type="button" onclick="selecionaDactes()">Editar</button></td>
</tr>
<tr>
<td><input class="editar" type="checkbox" id="701" value="701"></td>
<td>Santos</td>
<td><button type="button" onclick="selecionaDactes()">Editar</button></td>
</tr>
<tr>
<td><input class="editar" type="checkbox" id="33" value="33"></td>
<td>Londrina</td>
<td><button type="button" onclick="selecionaDactes()">Editar</button></td>
</tr>
</table>
I am new in javascript and am not finding the solution I’ve read the documentation on this Link, but I couldn’t find the solution
thank you very much, it was exactly my question.
– WMomesso
Now I have another problem regardless of the amount of data selected always carries two commas before the data, example if selecting two items is like ",33,67", which can be?
– WMomesso
@Wagnerfernandomomesso gives me the idea that you’re not using the selector I indicated:
'table :checked
. But if you want to put here a jsFiddle that I can take a look at.– Sergio
I did what you asked me https://jsfiddle.net/3mv1weL5/, and I did according to your indication anyway, my table is contained in a while and comes in php code I have already checked the whole php structure and it’s all right, if you say that jsFiddle is I will rewrite my code, thank you.
– WMomesso
@Wagnerfernandomomesso https://jsfiddle.net/5s9nwg8w/ <- works well.
– Sergio