0

This script causes the alert to show regardless of whether the checkbox is being ticked on or off. How can it only show when the tick box is being clicked on?

$("#x").click(function() {
    alert("Ticker is has been clicked on!");
});

JSFIDDLE

3 Answers 3

2

You can add an if statement inside the function to test for that:

$("#x").click(function() {
    if (this.checked) alert("Ticker is has been clicked on!");
});
Sign up to request clarification or add additional context in comments.

Comments

1

//use .is(':checked') to check if it was checked!

$("#x").click(function() {
    if($(this).is(':checked')){
        alert("Ticker is has been clicked on!");
    }
});

Comments

0

A simple if conditional will do the trick for you:

$("#x").click(function(){
    if($(this).is(":checked")) {
        alert("Ticker is has been clicked on!");
    }
});

Check out some reading on the .is() method and :checked selector within jQuery.

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.