Issue
I have this code and want to change text of each id when window loaded by targeting
function change_text() {
const incList = document.querySelectorAll("[id ^=\'inc_\']");
[].forEach.call(incList, function() {
const tID = this.getAttribute("target");
document.getElementById("inc_" + tID).innerText += tID;
});
}
window.onload = change_text;
<div id="inc_1" target="1">111</div>
<div id="inc_2" target="2">222</div>
<div id="inc_3" target="3">333</div>
<div id="inc_4" target="4">444</div>
<div id="inc_5" target="5">555</div>
But it doesn't work. Help, please!
Solution
First, this isn't bind on callback function in forEach.
You just need to put the parameter in callback function in forEach
function change_text() {
const incList = document.querySelectorAll("[id ^=\'inc_\']");
[].forEach.call(incList, function(element) {
const tID = element.getAttribute("target");
document.getElementById("inc_" + tID).innerText += tID;
});
}
window.onload = change_text;
<div id="inc_1" target="1">111</div>
<div id="inc_2" target="2">222</div>
<div id="inc_3" target="3">333</div>
<div id="inc_4" target="4">444</div>
<div id="inc_5" target="5">555</div>
Answered By - Kurt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.