Question:
I have this code
I want to get all p elements inside both divs to set different id attributes (or change the values of the class attributes if that is easier) to each of them so I can modify the styles in a css file. But I don´t know how to properly access the p elements inside the two divs
Answer:
You would need to get the div elements, then on those elements, you can do
querySelectorAll('p')
to get an array of the paragraphs.
Something like this.
If you just want to get all the paragraphs and you don’t care about where they are you can of course query them on the document object instead. You could also be more generic to access all paragraphs following div’s:
Essentially any valid CSS selector can be used with
querySelector
or
querySelectorAll
.
Without knowing what exactly you are trying to do, it would probably be better using classes for the divs, then you could query them at the same time, instead of having to query them separately.
If you have better answer, please add a comment about this, thank you!