5

I have some checkboxes that come from my database wich look like this:

<input type="checkbox" class="hidden chkbox" id="single_checkbox_<?php echo $page->ID; ?>"    name="pages[]" value="<?php echo $page->ID; ?>"/>
<label rel="tooltip" data-original-title="Selecteer" class="btn" for="single_checkbox_<?php echo $page->ID; ?>"><i class="icon-check-empty"></i> Selecteer</label>

Now the checkboxes are hidden because i want the label class to act as the checkboxes.

it is in fact working but the problem is that is want to change <i class="icon-check-empty"></i> after the checkbox is clicked to <i class="icon-check"></i> and back to icon-check-empty after unchecked.

The jquery i have so far is

$('.chkbox').click(function(){

        if($(this).is(':checked')){
            $('i.icon-check-empty').replaceWith('<i class="icon-check"></i>');

        }else{

            $('i.icon-check').replaceWith('<i class="icon-check-empty"></i>');

        }

    });

It does change however it changes all the checkboxes and that isn't what i'm looking for.

If anybody has an idea on how to help me that would be great.

Kind regards.

8 Answers 8

4

you can do this by

$("selector ").attr('class', 'newClass'); //set class (regardless of what it was)

or by

 $("selector ").addClass('newClass'); //add a class
 $("selector ").removeClass("second"); //remove class

list of jQuery methods specifically for the class attribute.


try

$('.chkbox').click(function () {
    var $this = $(this);
    if ($this.is(':checked')) {
        $this.next().find('i.icon-check-empty').replaceWith('<i class="icon-check"></i>');
    } else {
        $this.next().find('i.icon-check').replaceWith('<i class="icon-check-empty"></i>');
    }
});

and better approach is using toogle

$('.chkbox').click(function(){
     var $ico = $(this).next().find('i');
     $ico.toggleClass('icon-check icon-check-empty');
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your quick reply, i've tried it however this still changes all my icon classes. i just want the one that is checked to be changed.
2

jsBin demo

$('.chkbox').click(function(){
     var $ico = $(this).next('label').find('i');
     $ico.toggleClass('icon-check icon-check-empty');
});

Or like:

jsBin demo

function checkTest( chkbx ) {

    var $ico     = $(chkbx).next('label').find('i'),
        icoClass = ['icon-check', 'icon-check-empty'],
        io       = $(chkbx).is(':checked') ? 0 : 1 ;

    $ico.removeClass().addClass( icoClass[io] );

}


$('.chkbox').each(function(){
    checkTest( this );  // Test all on page load
}).click(function(){
    checkTest( this );  // Test on click
});

4 Comments

+1 - this is the way to do it. I just finished making a fiddle with what is essentially this. :)
Thanks, works perfectly.. it has been driving me crazy, after a while i am just trying the most weird sollutions haha. it was in fact very easy. thanks a million :D
@JarnovanRhijn added a demo where you don't need to assign classes to your icons in HTML and you can have already selected ones by just adding checked="checked" to the desired input
@roXon Thanks man.. getting better by the minute :) will implement this as the demo provides.. Again Thanks :D
2

Try

$('.chkbox').click(function(){
     var $this = $(this);
     if($this.is(':checked')){
         $this.next().find('i.icon-check-empty').replaceWith('<i class="icon-check"></i>');    
     } else {
         $this.next().find('i.icon-check').replaceWith('<i class="icon-check-empty"></i>');
     }
 });

Or better simply toggle class

$('.chkbox').click(function () {
    var $this = $(this),
        isChecked = $this.is(':checked');
    $this.next().find('i')
        .toggleClass('icon-check-empty', !isChecked)
        .toggleClass('icon-check', isChecked);

});

Comments

1

also look at .addClass() , .removeClass() , .toggleClass()

Comments

1

You can try this :

$('.chkbox').click(function(){

        if($(this).is(':checked')){
            $(this).next().find('i.icon-check-empty')attr("class",icon-check);

        }else{

            $(this).next().find('i.icon-check').attr("class",icon-check-empty);

        }

    });

Comments

1

you can do:

$('.chkbox').click(function(){
    var i = $(this).next().find("i");

    if($(this).is(':checked')) {
        i.removeClass("icon-check-empty");
        i.addClass("icon-check");
    }
    else {
        i.removeClass("icon-check");
        i.addClass("icon-check-empty");
    }
});

Comments

1

Try something like this

HTML

<input type="checkbox" class="chkbox" id="chkbox"  name="chkbox" value="value"/>

JQuery

$('.chkbox').click(function(){

    console.log('click');

    if($(this).is(':checked')){
        console.log('checked');
        $("#id").addClass('icon-check');
        $("#id").removeClass('icon-check-empty');
    }
    else{
        console.log('unchecked');
        $("#id").addClass('icon-check-empty');
        $("#id").removeClass('icon-check');
    }

});​

Comments

1

you can use removeClass and addClass to do what you want

$('.chkbox').click(function(){

        if($(this).is(':checked')){
           $(this).removeClass("icon-check-empty");
           $(this).addClass("icon-check");
        }else{
           $(this).removeClass("icon-check");
           $(this).addClass("icon-check-empty");

        }

    });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.