Find and display in the div all checked checkboxes with the button

I have a huge shape with many flags. In a specific area, I show the total that comes from the values โ€‹โ€‹of checked flags. I would like to display all the headers or alt of the checkboxes, as well as in a specific area (div) below my total. preferably using jQuery.

[SOLVED] This is what I expected. Finally decided:

$("input[type='checkbox']:checked").each(function(){ var checkAltValue = $(this).attr("value"); $('div#WHATEVER_DIV_ID').append(checkAltValue) }); 

You can also find better alternatives. Thanks guys for the violin examples.

+4
source share
4 answers

This will be a case of listening to the .click() event, and then, when called, find all checked flags and add to the div. Example below:

Demo

 $(document).ready(function(){ $('#myButton').click(function(){ var checked = $('#checked'); checked.children().remove(); $('input[type=checkbox]:checked').each(function(){ checked.append($(document.createElement('li')).text($(this).attr('title'))); }); }); }); 
+1
source

Add the following code to the button click handler to get the values โ€‹โ€‹of all the marked fields:

 $("input[type='checkbox']:checked").each(function(){ var checkAltValue = $(this).prop("alt"); //other code here }); 
+1
source

Here is a working fiddle that should help

and here is the code -

 $(function(){ $('#test').click(function(){ //#test being the id of the button you want to click var allTitles = $(':checkbox:checked').map(function(){ return this.alt }).get().join(); $('#myDiv').html(allTitles); }); 
+1
source

You can check all the checkboxes in your form by doing this:

 $("input[type=checkbox][checked]").each( function() { // Insert code here } ); 
0
source

Source: https://habr.com/ru/post/1486238/


All Articles