admin管理员组文章数量:1024670
What am i doing wrong here?
Here is the code:
<script>
var points = 1000;
document.getElementById(pointsdisplay).innerHTML = "Points:" + points;
</script>
<p id="pointsdisplay"></p>
I want to display the points on the website. I really don't understand why this isn't working.
Note that I put the script tag inside of the HTML code. Later, I will make an external js file and put everything in there.
What am i doing wrong here?
Here is the code:
<script>
var points = 1000;
document.getElementById(pointsdisplay).innerHTML = "Points:" + points;
</script>
<p id="pointsdisplay"></p>
I want to display the points on the website. I really don't understand why this isn't working.
Note that I put the script tag inside of the HTML code. Later, I will make an external js file and put everything in there.
Share Improve this question edited Apr 19, 2017 at 4:55 Gaurav Gandhi 3,2012 gold badges29 silver badges40 bronze badges asked Apr 19, 2017 at 3:00 user6184206user6184206 4- Put the id inside getElementbyId inside double quotes. – Om Sao Commented Apr 19, 2017 at 3:03
- I noticed that I forgot to put "" inside of when I was calling the id of pointsdisplay, after fixing this, it still doesn't do anything PLZ help! – user6184206 Commented Apr 19, 2017 at 3:03
- Quotes around getElementById("pointsdisplay") – zer00ne Commented Apr 19, 2017 at 3:03
-
Without quotes around it
pointsdisplay
is a variable. Since you haven't defined a variable calledpointsdisplay
, its value isundefined
, sogetElementById
is looking for an element named"undefined"
, not one named"pointsdisplay"
. Also, the JS is running before thep
exists, so it won't find it even if you put the quotes in. – Useless Code Commented Apr 19, 2017 at 3:08
6 Answers
Reset to default 1You need to define the DOM
in HTML before the script. Like this,
<p id="pointsdisplay"></p>
<script>
var points = 1000;
document.getElementById("pointsdisplay").innerHTML = "Points:" + points;
</script>
There are two mistakes here,
- You need to surround id with
"
(Double Quotes) - You should put script tag after the
dom
element is created, else it may execute before the actualdom
is created. Even if you keep js code in a separate file, make sure you write it after yourdom
, just before thebody
tag ends.
So your code should be like,
<p id="pointsdisplay"></p>
<script>
var points = 1000;
document.getElementById('pointsdisplay').innerHTML = "Points:" + points;
</script>
Working jsFiddle
You should be having the element id between quotations and besides, it is remended to have the DOM element before the script tag.
<p id="pointsdisplay"></p>
<script>
var points = 1000;
document.getElementById('pointsdisplay').innerHTML = "Points:" + points;
</script>
I think I see two problems here.
The first is that the document.getElementById needs to be given a String. You could use: document.getElementById("pointsDisplay")
.
Secondly, I think you will need to put the script underneath the p. The browser will execute the script as soon as it hits the script tag.
All of the other answers here are correct however I would like to point out that if you use the DOMContentLoaded event the script will wait until the DOM has loaded before executing.
Eg.
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var points = 1000;
document.getElementById("pointsdisplay").innerHTML = "Points:" + points;
});
</script>
<p id="pointsdisplay"></p>
The id name should be in string and the HTML tag should loaded first
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--you have two options -->
<p id="pointsdisplay"></p>
<!--Html tag should be loaded first -->
<script>
var points = 1000;
// the id name should be in string
document.getElementById("pointsdisplay").innerHTML = "Points:" + points;
</script>
<!-- second option jquery ready function, so that would wait for the document to be loaded then execute the script -->
<script>
var points = 1000;
$(document).ready(function(){
document.getElementById("pointsdisplay1").innerHTML = "Points:" + points;
});
</script>
<p id="pointsdisplay1"></p>
What am i doing wrong here?
Here is the code:
<script>
var points = 1000;
document.getElementById(pointsdisplay).innerHTML = "Points:" + points;
</script>
<p id="pointsdisplay"></p>
I want to display the points on the website. I really don't understand why this isn't working.
Note that I put the script tag inside of the HTML code. Later, I will make an external js file and put everything in there.
What am i doing wrong here?
Here is the code:
<script>
var points = 1000;
document.getElementById(pointsdisplay).innerHTML = "Points:" + points;
</script>
<p id="pointsdisplay"></p>
I want to display the points on the website. I really don't understand why this isn't working.
Note that I put the script tag inside of the HTML code. Later, I will make an external js file and put everything in there.
Share Improve this question edited Apr 19, 2017 at 4:55 Gaurav Gandhi 3,2012 gold badges29 silver badges40 bronze badges asked Apr 19, 2017 at 3:00 user6184206user6184206 4- Put the id inside getElementbyId inside double quotes. – Om Sao Commented Apr 19, 2017 at 3:03
- I noticed that I forgot to put "" inside of when I was calling the id of pointsdisplay, after fixing this, it still doesn't do anything PLZ help! – user6184206 Commented Apr 19, 2017 at 3:03
- Quotes around getElementById("pointsdisplay") – zer00ne Commented Apr 19, 2017 at 3:03
-
Without quotes around it
pointsdisplay
is a variable. Since you haven't defined a variable calledpointsdisplay
, its value isundefined
, sogetElementById
is looking for an element named"undefined"
, not one named"pointsdisplay"
. Also, the JS is running before thep
exists, so it won't find it even if you put the quotes in. – Useless Code Commented Apr 19, 2017 at 3:08
6 Answers
Reset to default 1You need to define the DOM
in HTML before the script. Like this,
<p id="pointsdisplay"></p>
<script>
var points = 1000;
document.getElementById("pointsdisplay").innerHTML = "Points:" + points;
</script>
There are two mistakes here,
- You need to surround id with
"
(Double Quotes) - You should put script tag after the
dom
element is created, else it may execute before the actualdom
is created. Even if you keep js code in a separate file, make sure you write it after yourdom
, just before thebody
tag ends.
So your code should be like,
<p id="pointsdisplay"></p>
<script>
var points = 1000;
document.getElementById('pointsdisplay').innerHTML = "Points:" + points;
</script>
Working jsFiddle
You should be having the element id between quotations and besides, it is remended to have the DOM element before the script tag.
<p id="pointsdisplay"></p>
<script>
var points = 1000;
document.getElementById('pointsdisplay').innerHTML = "Points:" + points;
</script>
I think I see two problems here.
The first is that the document.getElementById needs to be given a String. You could use: document.getElementById("pointsDisplay")
.
Secondly, I think you will need to put the script underneath the p. The browser will execute the script as soon as it hits the script tag.
All of the other answers here are correct however I would like to point out that if you use the DOMContentLoaded event the script will wait until the DOM has loaded before executing.
Eg.
<script>
document.addEventListener("DOMContentLoaded", function(event) {
var points = 1000;
document.getElementById("pointsdisplay").innerHTML = "Points:" + points;
});
</script>
<p id="pointsdisplay"></p>
The id name should be in string and the HTML tag should loaded first
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--you have two options -->
<p id="pointsdisplay"></p>
<!--Html tag should be loaded first -->
<script>
var points = 1000;
// the id name should be in string
document.getElementById("pointsdisplay").innerHTML = "Points:" + points;
</script>
<!-- second option jquery ready function, so that would wait for the document to be loaded then execute the script -->
<script>
var points = 1000;
$(document).ready(function(){
document.getElementById("pointsdisplay1").innerHTML = "Points:" + points;
});
</script>
<p id="pointsdisplay1"></p>
本文标签: javascriptdocumentgetElementById not workingDisplayStack Overflow
版权声明:本文标题:javascript - document.getElementById not workingDisplay - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745610024a2158948.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论