Is it possible to get a child dom element, which will be the first child of a specific type.
For example in both of these examples I want to get the img
ele
Try this
var firstIMG = document.getElementById('mylink').getElementsByTagName('img')[0];
Here is a javascript function
function getFirst(con,tag)
{
return con.getElementsByTagName(tag)[0];
}
//html markup
<a id="mylink" href="#"><img src="myimage.jpg" /></a>
<a id="mylink1" href="#"><img src="myimage.jpg" /></a>
//test
var con=document.getElementById('mylink');
var first=getFirst(con,'img');
You can also use querySelector:
var firstImg = document.querySelector("#mylink > img:first-of-type");
var anchor = document.getElementById('mylink');
var images = anchor.getElementsByTagName('img');
var image = images.length ? images[0] : null;