1

I have the following code

  var eventbar = document.createElement("Div");
  eventbar.id = "eventa";
  eventbar.className= "event";
  eventbar.onclick = 'createpopupdata(this)';
  eventbar.innerHTML = "Click here";
  document.getElementById("body").appendChild(eventbar); 

However when it creates the object inside the HTML my inspector shows no onClick property in the HTML Div that is created. Wondering were I'm going wrong as I have tried multiple ways posted here. (Obviously sans the quotes runs the function immediately).

Edit: I'm wonedering if I have to do something special like create a listener for the object or if there is an easy solution.

3
  • 1
    You can replace eventbar.onclick = 'createpopupdata(this)'; with eventbar.setAttribute('onclick', 'createpopupdata(this)'); Commented Jan 31, 2016 at 12:19
  • I think this is exactly what I was looking for as it will allow me to send multiple parameters(I hope). Commented Jan 31, 2016 at 12:20
  • It's also the way that leaves you with HTML that resembles (i.e is identical to) the HTML you'd get if you typed it out by hand and used an inline js handler, as you're doing. ;) Commented Jan 31, 2016 at 12:22

3 Answers 3

2

You can try something like this:

var eventbar = document.createElement("Div");
eventbar.id = "eventa";
eventbar.className = "event";
eventbar.onclick = createpopupdata.bind(null,eventbar);
eventbar.innerHTML = "Click here";
document.getElementById("body").appendChild(eventbar);

function createpopupdata(el){
  alert(el.id)
}
div{
  width:50px;
  height:50px;
  background:#eee;
}
<div id="body"></div>

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

1 Comment

I'm sorry but didn't get why the bind vs what was displayed below. Can you explain?
1

Your onclick (not onClick it's case-sensitive!) should be the reference to the function (createpopupdata) , not the call to it ('createpopupdata(this)'). First add it to the DOM, then the onclick listener.

function createpopupdata(event) {
  var el = event.target;
  alert(el.id + " clicked, foo: " + el.dataset.foo + ", bar: " + el.dataset.bar);
}
var eventbar = document.createElement("Div");
eventbar.id = "eventa";
eventbar.dataset.foo = "FOO";
eventbar.dataset.bar = "BAR";
eventbar.className= "event";
eventbar.innerHTML = "Click here";
document.getElementsByTagName("body")[0].appendChild(eventbar); 
eventbar.onclick = createpopupdata;

You also have to have <body id="body"> in order to make your version work. I modified it to work in the usual case.

4 Comments

Does not seem to work. (Also of course I have the body id=body tag)
Ok so this is working but how would one pass in multiple parameters with this method?
@Kevrone, see foo, bar added
Awesome. I actually used a combination of the dataset you mentioned as well as the setattribute mentioned by @enhzflep
0

This should work:

eventbar.addEventListener('click', function (){
    createpopupdate(this);
}, false);

2 Comments

Still no love with this one.
Please explain how this code solves the OP's question. This will help the OP understand your code and apply it to theirs, as well as help future searchers.

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.