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!!

Leave a Reply

Your email address will not be published. Required fields are marked *