0

This is the piece of code in question:

       ...     
        var btnClick = document.getElementById("button");
        btnClick .addEventListener("click", func, false);
    }
    function func()
    {
        alert("This works");
    }

I don't get any alert box. See any problems?

2
  • Do you see any errors in your JavaScript console? Also, are you sure that control flow passes through the code that adds the event listener? Commented Nov 28, 2011 at 5:10
  • The syntax is correct, it is a problem with your HTML, or maybe you have another error in your javascript that causes this code to never run. Commented Nov 28, 2011 at 5:12

4 Answers 4

1

Which browser are you using? IE doesn't support .addEventListener() until version 9, so you need to say something like this:

var btnClick = document.getElementById("button");

if (btnClick.addEventListener){
  btnClick.addEventListener('click', func, false); 
} else if (btnClick.attachEvent){
  btnClick.attachEvent('onclick', func);
}
// and if you're really keen:
else {
  btnClick.onclick = func;
}

And don't use .getElementById() on an element that hasn't been parsed yet, i.e., put the above code either in a document.ready or load event handler, or put it after the element in the page source.

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

4 Comments

Using firefox, but getElementById should work, since this is called after everything is loaded in the DOM
In that case I would expect your code to work unless there is an error somewhere in the lines above it. (Though you should do something like what I said above so that you support IE.)
Who cares about IE? Haha nah, this is a uni assignment...Chrome and FFox only required
Cool. If you're at the beginning of your degree maybe IE7 and 8 will be dead by the time you finish, but if you're near the end of your degree you'll need to know how to handle these cross-browser nuisances 'cause you'll need to do it when you get into the real world (assuming you intend to do this for a career). Still, I can understand just wanting to get the assignment done and submitted so I probably wouldn't bother with IE either if it isn't actually required. (Extra credit, maybe?)
0

Its because of the reason that you have not initialised it....!!!

window.onload = prepareButton;

function prepareButton()
{
    document.getElementById('button').onclick = function()
    {
        alert('you clicked me!');
    }
}

1 Comment

cannot use onlick...assignment directions
0

If you do all of the following, this will work:

  1. Wait for the document to load before trying to install the event listener. You can either run this code in a window.onload handler or place the javascript initialization code at the end of the body. You can verify if this is or isn't working by seeing if btnClick is valid or null.
  2. Only run this code in newer browsers that support addEventListener (old versions of IE don't support that).
  3. Make sure there is one and only one element in the page with id="button".
  4. Make sure there are no javascript errors in your page causing your script to stop running.
  5. Remove the space after btnClick before .addEventListener.

You can see it work here: http://jsfiddle.net/jfriend00/HQ24Z/

Comments

0

Never use very generic terms such as 'button' as they tend to be reserved and you'll end up ripping your hair out if you're absolutely sure it should work. Use descriptive values for id attributes such as 'form_4_button_submit'.

Also an example of how to do cross-browser event handling...

if (window.addEventListener) {document.addEventListener('keydown',keyPressed,false);}
else {document.attachEvent('onkeydown',keyPressed);}

function keyPressed(evt)
{
 var e = evt || event;
 var key = e.which || e.keyCode;

 switch (key)
 {
  case 77:// M
  alert('m key pressed');
  break;

  case 76://L
  alert('L key pressed');
  break;
 }
}

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.