0

Can I add an eventListener in JavaScript to a checkbox or do I have to use the attribute in html onclick = someFunc() to trigger a function. I set a up a fiddle where I tried to add an eventListener, but that does not give me the expected output.

var checkb = document.getElementById("checkbox1");

(function() {

    checkbFunc(checkb);

    function checkbFunc(checkb) {
        checkb.addEventListener("click", function(e) {
            if (checkb.checked) {
                alert("i am checked");

            } else {
                alert("i am not checked")
            }
        });
    }
});
4
  • You never execute the function. Do you mean to Immediately Invoke that Function Expression? Commented Sep 29, 2016 at 12:38
  • See this Demo! Commented Sep 29, 2016 at 12:38
  • @AndrewL. with other functions, written like this, it works! Commented Sep 29, 2016 at 12:40
  • 1
    (function() { ... }); never executes the anonymous function. But if you change it to (function() { ... })(); then it does and your fiddle works. Commented Sep 29, 2016 at 12:40

4 Answers 4

1

Yes, you can. Just unwrap your code from all the functions, and use the change event instead, and it should work fine

var checkb = document.getElementById("checkbox1");

checkb.addEventListener("change", function(e) {
    if (this.checked) {
        alert("i am checked");
    } else {
        alert("i am not checked")
    }
});

FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

Or execute the function expression using (function() { ... })();
0

You can do other way around like this

var checkb = document.getElementById("checkbox1");

(function() {

    checkbFunc(checkb);

    function checkbFunc(checkb) {
        checkb.addEventListener("click", function(e) {
            if( checkb.checked ) {
                alert("i am checked");

            } else {
                alert("i am not checked")
            }
        });
    }
})(); // <--added this    

Demo

Comments

0

You could simply remove your anonymous function

var checkb = document.getElementById("checkbox1");

checkbFunc(checkb);
    
function checkbFunc(checkb) {
  checkb.addEventListener("click", function(e) {
    if( checkb.checked ) {
      alert("i am checked");

    } else {
      alert("i am not checked")
    }
  });
}
<input type="checkbox" id="checkbox1"/>

Or, better, use JQuery document.ready block.

Comments

0

your code is not working because the main anonymous functions is not executed.

The code below should work for you.

var checkb = document.getElementById("checkbox1");

(function() {

  checkbFunc(checkb);

    function checkbFunc(checkb) {
        console.log(checkb);
        checkb.addEventListener("click", function(e) {
            if( checkb.checked ) {
                alert("i am checked");

            } else {
                alert("i am not checked")
            }
        });
    }
}() // Add these round braces );

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.