Select the parent element whose child element is going to be selected. Use . querySelector() method on parent. Use the className of the child to select that particular child.
The firstChild property returns the first child node of a node. The firstChild property returns a node object.
The :first-child checks if its subject is the first child of its parent. In thise case you are trying to select element with id #LeftScrollableDiv that is the first child of its parent. All the : selectors just filter previous selection, they don't select any new elements.
The firstElementChild property returns the first child element of the specified element. The firstElementChild property is read-only. The firstElementChild property returns the same as children[0].
Try this
var firstIMG = document.getElementById('mylink').getElementsByTagName('img')[0];
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With