How to know which checkboxes are selected?

1

How do I make a javascript code to know which checkboxes are selected and when I find it, get the value data-id of it?

<input type="checkbox" data-id="1">
<input type="checkbox" data-id="2">
<input type="checkbox" data-id="3">

I need something like this:

s = '';
for (checkbox){
  if (checkbok.checked) { s+= checkbox.data-id.val(); }
}
alert(s);
    
asked by anonymous 31.07.2018 / 17:53

4 answers

2

I added a test button so that you can understand that the var s statement must be inside the event so that there is no repeated result:

<input type="checkbox" data-id="1">
<input type="checkbox" data-id="2">
<input type="checkbox" data-id="3">
<button id="teste">teste</button>
<script type="text/javascript">
    $("#teste").click(function(){
        var s = '';
        $.each($('input[type="checkbox"]'),function(){
            if($(this).is(":checked")){
                s+=$(this).data("id");
            }
        });
        alert(s);
    });
</script>

@AndersonCarlosWoss Advice:

$.each($('input[type="checkbox"]:checked'),function(){
    s+=$(this).data("id");
});
    
31.07.2018 / 18:09
5

Just use querySelectorAll('input[type=checkbox]:checked') to select all fields marked and use the dataset attribute to access the value of data-id .

const checkboxes = document.querySelectorAll('input[type=checkbox]:checked');

for (let checkbox of checkboxes) {
  console.log(checkbox.dataset.id);
}
<input type="checkbox" data-id="1" checked>
<input type="checkbox" data-id="2">
<input type="checkbox" data-id="3" checked>
    
31.07.2018 / 18:09
1
var s = "";
$("input[type=checkbox]").each(function () {
    if ($(this).prop("checked")) {
        s += $(this).attr("data-id") + ",";
    }
});
alert(s);
    
31.07.2018 / 18:22
1
___ 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>
    
______ azszpr319025 ___

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
    
______ azszpr319024 ___

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
    
______ azszpr319029 ___
%pre%     
______ ___ azszpr319046

Also using .map () you results in a string with the values separated by comma. Example:

%pre% %pre%

You can get the %code% in two ways:

pure JavaScript: %code%

jQuery: %code%

Where:

%pre%     
___
31.07.2018 / 18:55