Skip to main content

JQuery - Output the class name of checked checkboxes



I am wondering whether or not it is possible to output the class name of checked checkboxes each time a checkbox is checked/unchecked? For example, I have 3 checkboxes. If I check one, it'll output its class name, if I then check a 2nd one it'll output the first checkbox class name + the 2nd class name. If I then uncheck the first checkbox, it'll only output the class name of the 2nd checkbox.. and so forth? I made a JSFiddle to get started... http://jsfiddle.net/LUtJF/





Thanks


Comments

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


    Your fiddle, fiddled with.

    ReplyDelete
  2. Check this fiddle: 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);

    ReplyDelete
  3. $(document).ready(function() {
    var cb = $('input[type=checkbox]');
    cb.change(function() {
    cb.each(function() {
    if ($(this).is(':checked')) {
    alert($(this).attr('class'));
    }
    });
    });
    });

    ReplyDelete
  4. It can be done like

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


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

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.