___ erkimt ___ How to know which checkboxes are selected? ______ qstntxt ___
How do I make a javascript code to know which checkboxes are selected and when I find it, get the value data-*
of it?
// Faz tudo de uma vez: declara a variável, cria a array,
// converte em string e atribui à variável
const s = $("[type='checkbox']:checked").map(function(){
return this.dataset.id;
}, []).get().join();
console.log(s); // imprime: 1,3
I need something like this:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="checkbox" data-id="1" checked>
<input type="checkbox" data-id="2">
<input type="checkbox" data-id="3" checked>
I added a test button so that you can understand that the elemento.dataset.NOME
statement must be inside the event so that there is no repeated result:
<input data-id> ← elemento
↑↑
NOME
@AndersonCarlosWoss Advice:
// Faz tudo de uma vez: declara a variável, cria a array,
// converte em string e atribui à variável
const s = $("[type='checkbox']:checked").map(function(){
return this.dataset.id;
}, []).get().join();
console.log(s); // imprime: 1,3
Just use $(seletor).data("NOME");
to select all fields marked and use the %code% attribute to access the value of %code% .
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputtype="checkbox" data-id="1" checked>
<input type="checkbox" data-id="2">
<input type="checkbox" data-id="3" checked>
<input data-id> ← elemento
↑↑
NOME
Also using .map () you results in a string with the values separated by comma. Example:
You can get the %code% in two ways:
pure JavaScript: %code%
jQuery: %code%
Where:
%pre%