0

I want to add an event listener to an id which then alters some css. But without using html onchange, I have this (code below) so far. I'm almost certain im using addEventListener wrong.

window.onload = function() {
    var checkbox = document.getElementById('no_photo');
    alert('win loaded');
    checkbox.addEventListener('click', function() {
      alert('event triggered');
      var photo = document.getElementById('photo_upload');
      if(photo.style.display != 'none') {
        photo.style.display = 'none';
      } 
      else if(photo.style.display == 'none') {
        photo.style.display = 'block';
      };
    });
  }
3
  • 5
    You need to specify click in quotes... checkbox.addEventListener("change", Check your console for errors Commented Dec 19, 2013 at 2:34
  • fixed by using 'change' not click thanks for the help guys Commented Dec 19, 2013 at 2:45
  • Cool, it should also work with 'click' though: jsfiddle Commented Dec 19, 2013 at 2:46

1 Answer 1

1

You wrote

checkbox.addEventListener(click, ...

instead of

checkbox.addEventListener('click', ...

Use your browser console to detect this kind of syntax errors.

You can open the console in most browsers by typing CTRL-SHIFT-i

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

1 Comment

Thanks, changing that still doesnt trigger the event :/

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.