-1

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!

2 Answers 2

2

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.

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

Comments

0

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.)

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.