admin管理员组文章数量:1306937
I'm trying to make a survey, and with each click, assign a variable a certain phrase. Then display it in the div
when I click span#display
.
I get the "Uncaught ReferenceError: Invalid left-hand side in assignment" when I run this code:
var a = "none chosen";
function panyPicka()
{
a= "I love Valve";
}
function panyPickb()
{
a= "I love Bethesda";
}
function panyPickc()
{
a= "I love EA";
}
function panyPickd()
{
a= "I love Ubisoft";
}
function panyPicke()
{
a= "I play terrible NES, SNES games";
}
function panyPickf()
{
a= "I like something that's so underground I wouldn't expect you to know it";
}
function display()
{
document.getElementById("display") = "a+''+b+''+c+''+d+''+e+''+f+''+g+''+h";
}
<div>
<span onclick="panyPicka(); this.style.border='inset';">
Valve <3</span>
<span onclick="panyPickb(); this.style.border='inset';">
Bethesda :\ </span>
<span onclick="panyPickc(); this.style.border='inset';">
EA </span>
<span onclick="panyPickd(); this.style.border='inset';">
Ubisoft </span>
<span onclick="panyPicke(); this.style.border='inset';">
LJN</span>
<span onclick="panyPickf(); this.style.border='inset';">
Other</span>
</div>
<span onclick="display();"> See Results </span>
<div id="display">
</div>
I'm trying to make a survey, and with each click, assign a variable a certain phrase. Then display it in the div
when I click span#display
.
I get the "Uncaught ReferenceError: Invalid left-hand side in assignment" when I run this code:
var a = "none chosen";
function panyPicka()
{
a= "I love Valve";
}
function panyPickb()
{
a= "I love Bethesda";
}
function panyPickc()
{
a= "I love EA";
}
function panyPickd()
{
a= "I love Ubisoft";
}
function panyPicke()
{
a= "I play terrible NES, SNES games";
}
function panyPickf()
{
a= "I like something that's so underground I wouldn't expect you to know it";
}
function display()
{
document.getElementById("display") = "a+''+b+''+c+''+d+''+e+''+f+''+g+''+h";
}
<div>
<span onclick="panyPicka(); this.style.border='inset';">
Valve <3</span>
<span onclick="panyPickb(); this.style.border='inset';">
Bethesda :\ </span>
<span onclick="panyPickc(); this.style.border='inset';">
EA </span>
<span onclick="panyPickd(); this.style.border='inset';">
Ubisoft </span>
<span onclick="panyPicke(); this.style.border='inset';">
LJN</span>
<span onclick="panyPickf(); this.style.border='inset';">
Other</span>
</div>
<span onclick="display();"> See Results </span>
<div id="display">
</div>
Share
Improve this question
edited Dec 15, 2015 at 1:48
SemicolonExpected
asked Dec 5, 2011 at 4:39
SemicolonExpectedSemicolonExpected
6142 gold badges12 silver badges38 bronze badges
2 Answers
Reset to default 4The following line is wrong:
document.getElementById("display") = "a+''+b+''+c+''+d+''+e+''+f+''+g+''+h";
You can't assign a string directly to an HTML element. One way to set the element's content is with .innerHTML
:
document.getElementById("display").innerHTML = "something";
Note also that the right-hand side of your expression is a string containing the actual text a+''+b+''+c+''+d+''+e+''+f+''+g+''+h
- that is, the character "a" then the character "+", then two apostrophes, etc. It is not an expression concatenating the variables a, b, c, etc. (not that your code as shown has variables for b, c, d, etc). If you remove the surrounding double-quotes it would be an expression concatenating the variables.
Having a different function for every option is a very messy way to code this type of thing. You may like to try something like the following (could be further tidied, but I don't want to overload you with too much information all at once):
var phrases = {
a : "I love Valve",
b : "I love Bethesda",
c : "I love EA",
d : "I love Ubisoft",
e : "I play terrible NES, SNES games",
f : "I like something that's so underground I wouldn't expect you to know it"
},
currentPhrase = "none selected";
function panyPick(which) {
currentPhrase = phrases[which] || "none selected";
}
function display() {
document.getElementById("display").innerHTML = currentPhrase;
}
<div>
<span onclick="panyPick('a'); this.style.border='inset';">
Valve <3</span>
<span onclick="panyPick('b'); this.style.border='inset';">
Bethesda :\ </span>
<span onclick="panyPick('c'); this.style.border='inset';">
EA </span>
<span onclick="panyPick('d'); this.style.border='inset';">
Ubisoft </span>
<span onclick="panyPick('e'); this.style.border='inset';">
LJN</span>
<span onclick="panyPick('f'); this.style.border='inset';">
Other</span>
</div>
<span onclick="display();"> See Results </span>
<div id="display"></div>
This line is incorrect:
document.getElementById("display") = "a+''+b+''+c+''+d+''+e+''+f+''+g+''+h";
use this:
document.getElementById("display").innerHTML = a+''+b+''+c+''+d+''+e+''+f+''+g+''+h;
Edit:
Actually this makes no scene: a+''+b+''+c+''+d+''+e+''+f+''+g+''+h
It is the same like this: a+b+c+d+e+f+g+h
本文标签: javascriptUncaught ReferenceErrorInvalid lefthand side in assignmentStack Overflow
版权声明:本文标题:javascript - Uncaught ReferenceError : Invalid left-hand side in assignment - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741825895a2399637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论