21

I have two functions.

The first function translates a div click into a checked/unchecked toggle. The second function translates a checkbox change into a hide/show event.

The problem is that when I use the first function to check/uncheck the box, the second function is not called. I am new to javascript, thanks.

<script type="text/javascript">
$(document).ready(function() {
    $(":checkbox").parent().click(function(evt) {
        if (evt.target.type !== 'checkbox') {
            var $checkbox = $(":checkbox", this);
            $checkbox.attr('checked', !$checkbox.attr('checked'));
            evt.stopPropagation();
            return false;
        }
    });
});
</script>

<script type="text/javascript">
$(document).ready(function() {
    $(":checkbox").change(function() {
        if($(this).attr("checked")) {
            $('.'+this.id).show();
        }
        else {
            $('.'+this.id).hide();
        }
    });
});
</script>

3 Answers 3

33

The change event does not fire when you programmatically change the value of a check box. What you can do to ensure it fires is:

 $(":checkbox").parent().click(function(evt) {
    if (evt.target.type !== 'checkbox') {
        var $checkbox = $(":checkbox", this);
        $checkbox.attr('checked', !$checkbox.attr('checked'));
        $checkbox.change();
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Your theory checks out and your solution works perfectly. Thanks.
Better to use (at least with some versions of jQuery) $.fn.prop vs $.fn.attr. Attr will assign as well as retrieve. Prop just retrieves.
5

Don't bother with the first snippet. Just use LABEL elements:

<label><input type="checkbox">Some option</label>

Now, when the user clicks the label (the text next to the checkbox), the checkbox will be activated.


The second snippet can be optimized:

$('input:checkbox').change(function() {
    $('#' + this.id).toggle(this.checked);
});

1 Comment

the $('.' + this.id) selector wont work. Need to use '#'. And we dont know whats in the div, you are assuming he is using it as a label, it might be something else.
1

you are using '.' which is for class selectors instead use '#' since you are using the element ID. Like this:

$(document).ready(function() {
    $(":checkbox").bind('change', function() {
        if($(this).attr("checked")) {
            $('#'+this.id).show();
        }
        else {
            $('#'+this.id).hide();
        }
    });
});

6 Comments

the OP did that on purpose.. ;) he's not hiding/showing the checkboxes but the <div> with a class equivalent to the id of the checkbox.
@Victor That wouldn't make sense, since the check-box is the element that got that ID.
@Sime I dont try to make sense of what they are trying to do. If thats the case it was unclear.
@Reigel Fair enough, but without the markup your guess is just as good as mine. But i guess it makes sense.
@Victor So when the visitor checks the check-box it should become visible? But it already was visible, otherwise the visitor couldn't have checked it. That does not make sense.
|

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.