Home > Software engineering >  How to get specific element inside of element stored in variable?
How to get specific element inside of element stored in variable?

Time:12-30

I am looking for way to select element inside of already located element in variable. For example I have selected a container:

let productAttachedFilesModal = document.getElementById('modal_more_information');

I know, that inside are array of a elements, which I want to select as well. Which method should I use? in jQuery there are method find(). So I need the analog for JS. Or I need to use DOM method again? Like this:

let listOfLinks= document.getElementById('modal_more_information > a');

CodePudding user response:

There are several ways:

let anchors = productAttachedFilesModal.getElementsByTagName("a");

let anchors = document.querySelectorAll("#modal_more_information > a")

let anchors = productAttachedFilesModal.querySelectorAll("a");

CodePudding user response:

You should use productAttachedFilesModal.children to get its children, which is the elements inside. It will get you an array of HTML elements.

CodePudding user response:

You can just query that stored DOM reference:

let productAttachedFilesModal = document.getElementById('modal_more_information');

// Query the stored DOM element for all descendent anchors
let childAnchors = productAttachedFilesModal.querySelectorAll("a");
  • Related