Reputation: 4239
I am trying to pass an argument to a onclick function using javascript
I had before
title = createElement('span', {className: 'title', innerHTML: this.Title,
ID: this.ID, Title: this.Title, onclick: TitleOnClick});
I am passing parameter 'test'
..
title = createElement('span', {className: 'title', innerHTML: this.Title,
ID: this.ID, Title: this.Title, onclick: TitleOnClick(test)});
function TitleOnClick(test){
alert(test);
}
Don't seem to work. Am I doing it wrong? Thanks a lot!
Upvotes: -1
Views: 51
Reputation: 413707
You can wrap the call to "TitleOnClick" in another function:
title = createElement('span', {className: 'title', innerHTML: this.Title,
ID: this.ID, Title: this.Title, onclick: function() { TitleOnClick(test); }});
(Assumes the variable "test" is declared somewhere, as did your example.)
Upvotes: 0
Reputation: 27236
When you call it that way, what you are doing is setting the value of onclick
equal to the RETURN value of TitleOnClick(test)
. What you probably want to do, instead, is something like:
onclick: TitleOnClick
(note no ()'s) - and then within that method, derive the value of test
based on context.
Upvotes: 2