2

I am trying to add click event listener to my divs that I am creating in my JS dynamically.

My Javascript snippet of function that is called each time to create the Div:

var listDiv = document.createElement("div");
listDiv.className = "list";
listDiv.addEventListener = ('click',gotoOutcomesLO, false);

The Function that is called by the click event:

function gotoOutcomesLO(e){
if(typeof(Storage)!=="undefined"){
        var ele = e.target;
        var text = ele.getAttribute("name");
        sessionStorage.test = text;
}
}

I don't see any click events added to my HTML and not sure what's wrong. Any pointers would be helpful! Thanks!

1
  • 2
    remove = in listDiv.addEventListener = ('click',gotoOutcomesLO, false); Commented May 27, 2014 at 5:44

3 Answers 3

6

Change

listDiv.addEventListener = ('click',gotoOutcomesLO, false);

to

listDiv.addEventListener('click',gotoOutcomesLO, false);
Sign up to request clarification or add additional context in comments.

Comments

2

addEventListener is a function remove equal:

var listDiv = document.createElement("div");
listDiv.className = "list";
listDiv.addEventListener('click', gotoOutcomesLO, false);

function gotoOutcomesLO(e) {
    if(typeof(Storage)!=="undefined") {
        var ele = e.target;
        var text = ele.getAttribute("name");
        sessionStorage.test = text;
    }
}

Also I guess you append listDiv to a node...

Comments

0

Here is javascript click event

var listDiv = document.createElement("div");
listDiv.className = "list";
listDiv.onclick = function(){
   alert('onclick fired')
}

2 Comments

don't use the onclick attribute :(
Using a chainsaw to cut meat works. This still doesn't mean we should abandon the knife.

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.