admin管理员组文章数量:1415145
<div id=name>Whats your Name?: <input type="text" id="User">
<input type="button" value="submit" onclick="giveUser()"/>
<br> I dont know your Name </br>
</div>
<br>
<div id=P2>
Oh its <span id=User>Default</span>!
</div>
<script>
function giveUser() {
var User = document.getElementById("User").value;
console.log(User)
document.getElementById('User').innerHTML = User;
document.getElementById('name').style.visibility = 'hidden';
document.getElementById('P2').style.visibility = 'visible';
}
</script>
Here i am asking the user there name through a textbox but it won't display it in the span.
here is a fiddle demo
<div id=name>Whats your Name?: <input type="text" id="User">
<input type="button" value="submit" onclick="giveUser()"/>
<br> I dont know your Name </br>
</div>
<br>
<div id=P2>
Oh its <span id=User>Default</span>!
</div>
<script>
function giveUser() {
var User = document.getElementById("User").value;
console.log(User)
document.getElementById('User').innerHTML = User;
document.getElementById('name').style.visibility = 'hidden';
document.getElementById('P2').style.visibility = 'visible';
}
</script>
Here i am asking the user there name through a textbox but it won't display it in the span.
here is a fiddle demo
Share Improve this question asked Feb 21, 2015 at 3:18 pfychpfych 9281 gold badge13 silver badges32 bronze badges4 Answers
Reset to default 4ID
attributes should be unique. Your span
and input
both have an ID of User
Change the ID
of one and then try again. (https://jsfiddle/k04pvhug/1/)
In addition, you should enclose all your attributes with quotes ("
). It's not required, but it looks cleaner ;)
You need quotes around the id's.
<span id="someid" />
Then JavaScript can access the DOM and set innerHTML
Also, P2 is a pre-defined setting, so it's not aviable as an id anyway. try "paragraphTwo"
or something.
Give the quotes around id and make sure that each attribute has the unique id like this
<span id="id" />
<p id="paragraph" />
<span id="span2" />
Use a different name for the var and the id
function giveUser() {
var User = document.getElementById("User").value;
console.log(User)
document.getElementById("demo").innerHTML = User ;
document.getElementById('name').style.visibility = 'hidden';
document.getElementById('P2').style.visibility = 'visible';
}
#P2 {
visibility: hidden;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id=name>Whats your Name?:
<input type="text" id="User">
<input type="button" value="submit" onclick="giveUser()" />
<br>I dont know your Name</br>
</div>
<br>
<div id=P2>Oh its <span id="demo">Default</span>!</div>
本文标签: javascriptDisplay a var in a spanStack Overflow
版权声明:本文标题:javascript - Display a var in a span - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745196802a2647181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论