Codereerer
Codereerer

Reputation: 33

Javascript parameter is not defined

So I try to make a collection of buttons, which print out a hello and the name of the button. The code below doesn't work because "Uncaught ReferenceError: Mike is not defined". Its a parameter, how do I even define it?

<input type="button" value="Say hello to Mike" onClick="hello(Mike);" />

and the JS is something like this:

function hello(name) {
... }

I have another button where clicking doesnt send any parameters and it works just as intended.

edit: Putting Mike in quotations produces another error: "Uncaught SyntaxError: missing ) after argument list". I am doing this as a homework and we are required to write the html with javascript, below is my modified, whole line of code which is throwing the error.

 document.write('<input type="button" value="Say hello to Mike" onClick="hello('Mike');" />');

could the document.type be the cause of the problem? Between these 2 errors, I do literally nothing else than add the quotation marks in the code.

edit2: It finally worked after I added \ before the quotation marks.

Upvotes: 3

Views: 13001

Answers (3)

Juincen Lee
Juincen Lee

Reputation: 1

when you user the function hello,param 'Mike' error, the correct writer:

you write this,system think it's a variable

Upvotes: 0

prasanth
prasanth

Reputation: 22490

Declare as a String in Mike .In your code The mike act like a variable .So the java-script check this variable defined or not. So only Its throw the Mike is undefined.

function hello(name) {
console.log(name)
}
<input type="button" value="Say hello to Mike" onClick="hello('Mike');" />

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

Since Mike is a string, correct syntax would be

onClick="hello('Mike');"

Since there are no quotes, javascript treating that as a variable.

Upvotes: 4

Related Questions