I want to clone only the images whose attribute alt
is "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 should check if it matches the condition if
. But it is not working yet as it should and in the console it does not show any syntax errors.