2
I want clone only images whose the attribute alt
be "photo".
Code
var str = document.getElementById('A');
var clone = str.cloneNode(true);
document.getElementById('B').appendChild(clone);
<span id='A'>
<p><img src="https://sites.google.com/site/mplayerplugin/thumbnails/1.jpg" alt="foto" /></p>
<p><img src="https://sites.google.com/site/mplayerplugin/thumbnails/2.jpg" alt="foto" /></p>
<p><img src="https://sites.google.com/site/mplayerplugin/thumbnails/3.jpg" alt="foto" /></p>
<p><img src="https://sites.google.com/site/mplayerplugin/thumbnails/4.jpg" alt="poster"/></p>
<p><img src="https://sites.google.com/site/mplayerplugin/thumbnails/5.jpg" alt="figura"/></p>
</span>
<hr>
<span id='B'> </span>
What I did was:
var item = document.getElementById('A').getElementsByTagName('p');
for (var i = 0; i < item.length; i++) {
var str = item[i].getElementsByTagName('img')[0].alt;
}
if(str == "foto") {
var clone = str.cloneNode(true);
document.getElementById('B').appendChild(clone);
}
For this to happen, I must check if it matches the condition if
. But it’s not working yet as it should and the console shows no syntax errors.