Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get first child node of specific type - JavaScript

Tags:

javascript

dom

People also ask

How do I select a specific child in Javascript?

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.

Which property would you use to get the first child node of a node?

The firstChild property returns the first child node of a node. The firstChild property returns a node object.

How do I get a div for my first child?

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.

What is firstElementChild in Javascript?

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;