There are two parts of HTML, i want get the hrefs from first part then use them for img src in the second part.
const links = document.getElementsByClassName('myImg');
for (let i=0; i <links.length; i++) {
let classValue = [ ];
classValue += links[i].getAttribute('href');
myDiv = document.getElementsByClassName('myDiv');
for (let i=0; i<myDiv.length; i++) {
const myImg = document.createElement("IMG");
myImg.setAttribute('src', 'classValue[i]');
myDiv[i].appendChild(myImg);
}
}
<a class="myImg" href="1.jpg"> 1.jpg </a>
<a class="myImg" href="2.jpg"> 2.jpg </a>
<a class="myImg" href="3.jpg"> 3.jpg </a>
<div class="myDiv"> </div>
<div class="myDiv"> </div>
<div class="myDiv"> </div>
This code doesn’t work.
I think I don't understand JS for loop well, the result i get from first part - "classValue", looks not right.
I think "classValue" is not a array, so can't use it in second part.
How can i get a array for the loop?
Can someone explain this for me.
classValue += links[i].getAttribute('href');? To add a string to the array?