3

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

8
  • Do you know the name of the JS variable at the time you are adding this attribute? Commented Nov 18, 2013 at 18:14
  • Did you try myFunction(cross) ? Commented Nov 18, 2013 at 18:18
  • do not set events with setAttribute! Commented Nov 18, 2013 at 18:18
  • and how to do it if i shouldn't set events with setAttribute? Commented Nov 18, 2013 at 18:24
  • 1
    Create a global variable inside the javascript scope, and reference the variable inside the function. if you need the value. del.setAttribute("onClick","myFunction("+VARIABLE+");"); Commented Nov 18, 2013 at 18:31

2 Answers 2

8

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();
});
Sign up to request clarification or add additional context in comments.

Comments

-3

You retrieve you local storage values and pass them as string to you function by simply:

    this.getElementById('button1').setAttribute("onClick","alert("+variableCross.toString()+");");

here is the example:

http://jsfiddle.net/ZyDL4/

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.