0

I'm trying to add a onclick event to a tag, but it isn't working. Can anyone tell me the correct way to add an onclick to this tag in js?

 var cell = result.appendChild(document.createElement('abbr')); cell.title = "cell number 1"; cell.className = 'chart-cell-type'; cell.addEventListener='onclick',(alert("clicked!")); //broken 
3
  • 1
    You're more likely to get help if you're more specific than "isn't working", both in what behavior you want and what you expect. Commented Dec 21, 2014 at 0:40
  • 2
    I think you need to look into the basics of JavaScript syntax a little bit more. addEventListener is a method that takes two parameters, a string and a function. Commented Dec 21, 2014 at 0:41
  • @Jeffery: the onclick event isn't being added to the element. Commented Dec 21, 2014 at 0:52

1 Answer 1

5

As Alexis points out, your syntax is wrong. You want something like:

cell.addEventListener("click", function() {alert("clicked");}) 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.