I have created a simple html and javascript page where there are two a tags. Upon pressing the READ ME button, the first a tag gets populated with a random number
Upon pressing the POPULATE button, I am trying to read to the value of the first a tag and populate the second a tag with the value
But its not working. The second a tag is getting populated with a UNDEFINED value.
Why is that?
<html>
<body>
<p>
<a id="first"></a><br>
<a id="second"></a><br>
<button id="readme" onclick="readme()">Read Me</button><br>
<button ="populate" onclick="populate()">Populate</button>
</p>
</body>
<script>
function readme()
{
document.getElementById("first").innerHTML = Math.round(Math.random()*100);
}
function populate()
{
var x = document.getElementById("first").value;
document.getElementById("second").innerHTML = x;
}
</script>
</html>
答案 0 :(得分:1)
您需要使用innerHTML:
function populate(){
var x = document.getElementById("first").innerHTML;
document.getElementById("second").innerHTML = x;
// you can also directly set values instead of using temp variable
document.getElementById("second").innerHTML = document.getElementById("first").innerHTML;
}