0

I have a listener on DOMContentLoaded which calls startup() function.

I want to add more listeners (within the startup function) to items added to the DOM in the startup function. But it seems to fail (no click event registetered)

I use item.addEventListener("click", f, false); where f is the function that is supposed to run.

NOTE: I CANNOT use onclick=... I also cannot use JQuery.

Demo code is here

5
  • Show some code and markup, a minimal example will do. Commented Nov 28, 2011 at 5:58
  • My code: docs.google.com/document/d/… Commented Nov 28, 2011 at 6:15
  • It is far better to post a minimal example here. It is very likely that in creating the example, you will discover your issue. If not, the example (and probably a solution) will be available for posterity. Otherwise, there will likely be a vague question with a dead link and an incongruent answer. Commented Nov 28, 2011 at 6:21
  • Why do you have "false" for third argument? Commented Nov 28, 2011 at 6:53
  • @tekknolagi: That argument defines whether the handler should be triggered in the capture or bubble phase. false means bubble phase. Commented Nov 28, 2011 at 8:09

2 Answers 2

1

You have several issues in your code:

  1. previous function is not defined
  2. To attach a click event via addEventListener, the event name is click not onclick.
  3. You are calling addListeners outside of your load function.

Here is a jsfiddle that works: http://jsfiddle.net/VUgRu/

You should always use the javascript console to see what errors you are getting.

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

Comments

0

Using no frameworks (no excuse to use them) and adding an event listener in standard compliant browsers and Internet Explorer 8 and older for attachEvent...

 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.