admin管理员组文章数量:1404923
I am running a function that populates several paragraph tags on a webpage with the days of the week. The function takes an array of days which is populated based on the current day (i.e. if today is Thursday, get the next 7 days in order).
My problem is that for some reason, I am unable to populate these tags. I have checked to make sure that the array is getting populated and the list is being iterated through. While both of these are true, none of the Inner HTML of the tags are being changed.
HTML:
<p class="mb-1" id="forecastDay1" runat="server">Day</p>
<p class="mb-1" id="forecastDay2" runat="server">Day</p>
JavaScript Function:
function populatePage(arry) {
var i;
for (i = 0; i < arry.length - 1; i++) {
var id = "forecastDay" + i.toString();
document.getElementById(id).innerHTML = arry[i].toString();
}
}
I have also tried it this way:
document.getElementById("forecastDay" + i.toString()).innerHTML = arry[i].toString();
I am running a function that populates several paragraph tags on a webpage with the days of the week. The function takes an array of days which is populated based on the current day (i.e. if today is Thursday, get the next 7 days in order).
My problem is that for some reason, I am unable to populate these tags. I have checked to make sure that the array is getting populated and the list is being iterated through. While both of these are true, none of the Inner HTML of the tags are being changed.
HTML:
<p class="mb-1" id="forecastDay1" runat="server">Day</p>
<p class="mb-1" id="forecastDay2" runat="server">Day</p>
JavaScript Function:
function populatePage(arry) {
var i;
for (i = 0; i < arry.length - 1; i++) {
var id = "forecastDay" + i.toString();
document.getElementById(id).innerHTML = arry[i].toString();
}
}
I have also tried it this way:
document.getElementById("forecastDay" + i.toString()).innerHTML = arry[i].toString();
Share
Improve this question
edited Aug 23, 2018 at 22:29
ibrahim mahrir
31.7k5 gold badges49 silver badges78 bronze badges
asked Aug 23, 2018 at 22:21
Dr_Berry721Dr_Berry721
692 silver badges12 bronze badges
3
-
When does the script run? Are you sure the elements are part of the DOM when it does run? Have you tried
console.log()
to see whatdocument.getElementById()
is returning? – Pointy Commented Aug 23, 2018 at 22:23 - Possible duplicate of Why does jQuery or a DOM method such as getElementById not find the element? – Heretic Monkey Commented Aug 23, 2018 at 22:27
- This function runs after the array is populated. The array is populated on the Page Load. I just tried the console.log and am getting a type error on the getElementById(); – Dr_Berry721 Commented Aug 23, 2018 at 22:30
2 Answers
Reset to default 2You do not need toString
in JavaScript.
Also, arrays start at 0
, while your elements start at 1
. You have to pensate that difference.
function populatePage(arry) {
var i;
for (i = 0; i < arry.length; i++) {
var id = "forecastDay" + (i + 1);
document.getElementById(id).innerHTML = arry[i];
}
}
// Test it
populatePage(["Day 1", "Day 2"]);
<p class="mb-1" id="forecastDay1" runat="server">Day</p>
<p class="mb-1" id="forecastDay2" runat="server">Day</p>
There are a few things wrong with your code:
- The first value of
i
is0
. Since there is not element#forecastDay0
,getElementById
will returnnull
. Trying to accessinnerHTML
ofnull
will throw an error, thus breaking thefor
loop and every thing after it. You should check your console when something doesn't work, it's a great way to debug. - The check for the
for
loop should bei < arry.length
noti < arry.length - 1
. - You don't need any of the
toString
calls.
That being said, here is how it should be:
function populatePage(arry) {
var i;
for (i = 0; i < arry.length; i++) {
var id = "forecastDay" + (i + 1); // i + 1 instead of just i. The casting to strings is done automatically
document.getElementById(id).textContent = arry[i]; // use textContent as there isn't really any HTML
}
}
本文标签: htmlPass variable into documentgetElementById()JavascriptStack Overflow
版权声明:本文标题:html - Pass variable into document.getElementById() - Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744864531a2629269.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论