Skip to content
Advertisement

Javascript select nested class element

I’m working on changing some elements the slider on my website

my slider code looks like this:

JavaScript

I would like to change price when the currency changes. To do that I would like to use javascript with getElementsByClassName and then innerHTML. However my javascript doesn’t work as wanted. Here it is

JavaScript

any suggestions to how could I separately address “sl_price” class for every “cl” element? Cheers

Advertisement

Answer

getElementsByClassName returns a collection (list) of elements (will therefore not have a innerHTML property)

You could try document.querySelector(".cl1 .sl_price") instead (takes a css selector and returns the first match)

read more at https://developer.mozilla.org/en-US/docs/Web/API/Document.querySelector

The end result would then be something like this;

JavaScript

Note: I am assuming you only wanted to match a single element. If not, you should look at @Bommox’s answer and querySelectorAll.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement