How to select an element inside another element | JavaScript

/ #JavaScript


Sometimes you want to select an element inside another HTML element, but how do you do it?

First, how do you select one element?
var el = document.getElementById("search-field");

This will give you an element with the id attribute set to "search-field".

Selecting multiple elements inside a div

Let's say that you have a div with a div inside. This div has multiple divs inside it and you want to select some of them.

var products = document.getElementById("products-wrapper").querySelectorAll(".product");

This will give you a list of all elements inside "products-wrapper" with the class attribute set to "product".

Comments

No comments yet...

Add comment

Newsletter

Subscribe to my weekly newsletter. One time per week I will send you a short summary of the tutorials I have posted in the past week.