0

I want to do some actions when a button on my page is clicked, but my function executes without clicking on the button and when I check the log using firebug I got "undefine" message. Below is my code snippet so far.

var email = [email protected]   
"<button class='btn btn-warning' onclick ='"+addNote(email)+"' >Add Note</button>"

function addNote(youremail){
     alert("Your email is "+ youremail);
}

With the above code snippet the output will be Your email is [email protected].

1

1 Answer 1

3

That is because you are calling the function instead of adding it into your string.

"<button class='btn btn-warning' onclick ='addNote(email)' >Add Note</button>"

See this example:

http://jsfiddle.net/sP35v/

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

4 Comments

will this not treat the parameter as a regular string instead of a variable?
@fanbondi No, it will treat it as a variable. If email were a string your event would look like this: onclick="addNote('email')" -> See the difference?
Oh okay I got it,however in the console it says email is not define even-though I defined it before calling it.
@fanbondi Did you quote that example email address? Please see the jsfiddle link I included in my post. It will show a working version.

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.