2

I want to trigger an event not manually but by jQuery itself.
Here understand my problem better:

$('.commonCheck').change(function () {
            if ($(".cashierCheck").attr("checked")) {
                $('.cashierInput').fadeIn();
                console.log("________________hr")
            }
            else{
                $('.cashierInput').fadeOut();
                console.log("_______________uncjecked")
            }
}

My checkbox:

<input type="checkbox" name="cashierFilter" id="cashierFilter" class="cashierCheck commonCheck" class="mrL0" />

When I manually check/uncheck the checkbox the change event is triggered without any problem. But I want the event to be happened when I somehow execute this:

$('#cashierFilter').prop('checked','true');

2 Answers 2

2

Demo http://jsfiddle.net/ga9nn/

You can use trigger to fire events programatically:

$('#cashierFilter').prop('checked', true).trigger('change');

Also, you should use is to check the attributes of an element, and you can cache selectors in a variable to improve performance:

$('.commonCheck').change(function () {
    var $cashierCheck = $(".cashierCheck");
    if ($cashierCheck.is(":checked")) {
        $cashierCheck.fadeIn();
        console.log("________________hr")
    }
    else {
        $cashierCheck.fadeOut();
        console.log("_______________uncjecked")
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Bruvoo, added a demo, feel free to use it, sorry I touched your post yo! :) Woot up anyways. "demo* is with document .ready
@Tats_innit nice one, my brother from another mother.
2

Use "is" in jquery the best solution:

$('.commonCheck').change(function () {
    if ($(".cashierCheck").is(":checked")) {
                $('.cashierInput').fadeIn();
                console.log("________________hr")
            }
            else{
                $('.cashierInput').fadeOut();
                console.log("_______________uncjecked")
            }
});

http://jsfiddle.net/PpcLe/

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.