12

How can I run a function on checkbox change?

I'm trying to write small checkbox replacing function base on that - but I'm doing something wrong.

Code:

(function($) {
    $.fn.checking = function() {
        if ($('.checbox').prop('checked', false) === true) {
            $('.new-checkbox').prop('id', 'unchecked');
        } else {
            $('.new-checkbox').prop('id', 'checked');
        }
    };
})(jQuery);


$(document).ready(function() {

    $('.checkbox').wrap('<div class="checkbox-wrapper"></div>');
    $('.checkbox-wrapper').append('<p class="new-checkbox">Blue = Checked, Red = Unchecked</p>');
    $('.checkbox').checking();
    $('.checbox').bind('change', function() {
        $(this).checking();

    });

});

PlayGround: LIVE DEMO

1
  • Quit tagging your titles please. We already have tags. Commented Jul 4, 2011 at 13:09

5 Answers 5

20

Is this what you want? I am under the impression that you are over-complicating things.

$('input[type="checkbox"]').change(function() {
    alert ("The element with id " + this.id + " changed.");
});
Sign up to request clarification or add additional context in comments.

3 Comments

input:checkbox selector will do just fine
@jAndy: Copy/paste will do just fine too. :)
Yes. I've actually tried that one before - but did not work. I know know there an issue with if/else statement not .change(); Thanks!
13

You have some typos and errors in your script.

if($('.checbox').prop('checked', false) === true)
//         ^                   ^^^^^^^^^^^^^^^^^

should be

if(this.prop('checked'))

if($('.checkbox').prop('checked')) would work too of course, but why select the element again, if you already made it a plugin and the element is available via this?

And

$('.checbox').bind('change', function(){
//      ^

should be

$('.checkbox').bind('change', function(){
//      ^

Working demo: http://jsfiddle.net/fkling/yGBAK/40/

That said, it is better (more logical) to add and remove a class than changing the ID of the element.

1 Comment

I was just about to look for an errors in the statement! Thanks Felix.
9
$('.checkbox').change(function(){
    alert('changed');
})

I usedthe class you placed on the checkbox (.checkbox) for this. It detects a change in the state of the checkbox (this works for other inputs as well)

The example I included alerts 'changed' one the checkbox is clicked or unclicked.

If you want to know if it is checked or not every time then:

$('.checkbox').change(function(){
    if($(this).is(':checked')){
        alert('checked');
    } else {
        alert('not checked');
    }
});

Comments

3

Try this:

$('input[type=checkbox]').change(function() {
    alert("I'm changed!");
});

Or

$('input[type=checkbox]').change(function() {
    if ($(this).is(':checked')) {
        alert('I\'\m --> checked');
    } else {
        alert('I\'\m --> unchecked');
    }
});

Comments

0

In your example the selector uses checbox, should be ".checkbox"

I modified your example so it works: http://jsfiddle.net/yGBAK/41/

I swapped the ids for elements.

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.