JQuery - display class name of checked flags

I am wondering if it is possible to display the class name of checked checkboxes every time the checkbox is checked or not checked? For example, I have 3 checkboxes. If I check one, it will output its class name, if I then draw a second, it will output the first flag class name + the name of the second class. If I uncheck the first flag, it will only output the class name of the second flag .. and so on? I did a JSFiddle to get started ... http://jsfiddle.net/LUtJF/

thanks

+4
source share
4 answers
$("input[type='checkbox']").change(function() { var classes = $("input[type='checkbox']:checked").map(function() { return this.className; }).get().join(","); alert(classes); }); 

Your violin, with a bang.

+4
source

Check out this script: http://jsfiddle.net/eUse5/

code:

 function showChecked() { var s = ''; $('input:checked').each(function() { if(s!='') s += ', '; s += $(this).attr('class'); }); alert(s); } $('input[type="checkbox"]').change(showChecked); 
0
source
 $(document).ready(function() { var cb = $('input[type=checkbox]'); cb.change(function() { cb.each(function() { if ($(this).is(':checked')) { alert($(this).attr('class')); } }); }); }); 
0
source

It can be done as

 $(":checkbox").click(function(){ var classes = ""; $(':checked[class]').each(function(){ // this will filter only checked checkbox having class attribute classes += " "+$(this).attr("class"); }); alert(classes); }); 

script: http://jsfiddle.net/LUtJF/7/

0
source

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


All Articles