setAttribute onClick with a variable as parameter?

Dwadelfri picture Dwadelfri · Nov 18, 2013 · Viewed 21k times · Source

Is this possible? And if so, how?¨ Is it possible to setAttribute onclick=myFunction() with a variable as parameter?

var variableCross = localStorage.getItem('cross'+localStorage.getItem('number'));
del.setAttribute("onClick","myFunction(variableCross);");

myFunction(variableCross); sends variable like a string? doesn't it?

thx

Answer

maček picture maček · Nov 18, 2013

Assuming this bit of code does what you want

var variableCross = localStorage.getItem('cross'+localStorage.getItem('number'));

Then this should work for you

del.addEventListener("click", function(event) {
  myFunction(variableCross);
  event.preventDefault();
});