admin管理员组文章数量:1357153
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.
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.
Share Improve this question edited Mar 25, 2017 at 7:25 Codereerer asked Mar 25, 2017 at 6:39 CodereererCodereerer 331 gold badge1 silver badge5 bronze badges 5-
Put single quotes around
Mike
, if it's to be parsed as a literal. – cartant Commented Mar 25, 2017 at 6:40 - Put Mike in single quotes – Shinigami Commented Mar 25, 2017 at 6:41
-
As is apparent by the syntax coloring your string does not include Mike. To use the
'
character in a string bounded by the same character you need to escape it i.e.... hello(\'Mike\')...
– Kiren Commented Mar 25, 2017 at 7:13 - Try this document.write('<input type="button" value="Say hello to Mike" onClick="hello(\'Mike\');" />'); – Vishal Commented Mar 25, 2017 at 7:13
- Vishal: that works. Goddamn, what a stupid error.. Thanks everyone! – Codereerer Commented Mar 25, 2017 at 7:22
3 Answers
Reset to default 4Since Mike is a string, correct syntax would be
onClick="hello('Mike');"
Since there are no quotes, javascript treating that as a variable.
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');" />
when you user the function hello,param 'Mike' error, the correct writer:
you write this,system think it's a variable
本文标签: Javascript parameter is not definedStack Overflow
版权声明:本文标题:Javascript parameter is not defined - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744073202a2586263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论