Select the Element Inside The Div (Division)

In many cases, we might end up with having same element ID in DOM, however, this is not recommended. In such scenario, if we want to perform certain scripting on one element, this may effects to another as well because of same element ID.

You can select the particular element based on div as shown.

var childElement = document.getElementById("parentDivId").querySelector("#childElementID");
// here both are using element ID. don't forget # before ID

Similarly, we can use className.

var childElement = document.getElementById("parentDivId").querySelector(".childElementID");
// parent id and child class name
/// other option
var childElement = document.querySelector("#parentDivId").querySelector("#childElementID");
// both elements ID
//// You can use . for class name and # for Id of the element.

Cheers!!

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s