Select through js | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 10

Select through js

In the code below how can I select all span tag using JAVASCRIPT just like CSS " p span {//codes} ". Note: not using any class name or id. https://code.sololearn.com/W9vHIyf0VbVg/?ref=app

4th Aug 2020, 7:03 PM
Kashyap Kumar
Kashyap Kumar - avatar
15 Answers
+ 6
If you won't to do "css like selection" from JS then querySelector & querySelectorAll are the best options I would suggest you.
4th Aug 2020, 8:31 PM
Raj Chhatrala
Raj Chhatrala - avatar
+ 4
it will get all the span within #cont Container and change the background color of 3rd span element https://code.sololearn.com/Whc3UwXYjZWe/?ref=app
4th Aug 2020, 7:27 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
document.getElementsByTagName("span");
4th Aug 2020, 7:09 PM
Artur
Artur - avatar
+ 3
Artur in the question he has mentioned to select all span, so I used map to change the background color, of all element.. And then later in the comment he said to target single element of that span, so I just added if condition to same code
4th Aug 2020, 7:55 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
TRY THIS document.querySelector('p span');
5th Aug 2020, 5:07 AM
MSN
MSN - avatar
+ 2
Artur no, I know this one... something else like in CSS div p{} edit: what if I want to select span in third p tag. We can do ....tagName("span")[3], something like this.. but when the code is 1000 lines code, there is a chance of having a lot of p or span tag..in this case we don't know the correct index of the element. There may be a alternative way for this,. I'm finding that.
4th Aug 2020, 7:10 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 2
Artur Check the edit.
4th Aug 2020, 7:16 PM
Kashyap Kumar
Kashyap Kumar - avatar
+ 2
Artur K@$hy@p~Kum@r®™[✓] ya we need to work with index, BUT that index we definitely know because it is in #cont container, and that being unique, we know the element position.. Even in css we use nth-child or the even odd pseudo classes so that we can target specific child element which are repeating.. there as well we provide the index.. The sole purpose of id and classes are to distinguish elements and categorise them with similar classes.. so we need to use them If not so, then to target a specific element just by tags are not easy. Edit: For ex : <div> <p>Para from div 1</p> <p>Para from div 2</div> </div> <div> <p>Para from div 2</p> <p>Para from div 2</div> </div> <div> <p>Para from div 3</p> <p>Para from div 3</div> </div> How can we target the second div paragraph 2? either by index, or ID and classes.
4th Aug 2020, 7:37 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 2
Use document.querySelectorAll(span)
5th Aug 2020, 1:39 AM
Divya Mohan
Divya Mohan - avatar
+ 1
You mean you want to get all p tags inside div tag?
4th Aug 2020, 7:12 PM
Artur
Artur - avatar
+ 1
element.getElementsByTagName(" "); "element" - is the element from where the search starts. Only the element's descendants are included, not the element itself.
4th Aug 2020, 7:16 PM
Artur
Artur - avatar
+ 1
Мг. Кнап🌠 you still have to work with index in your if statement in map method
4th Aug 2020, 7:32 PM
Artur
Artur - avatar
+ 1
why would you use map method, spread and all that stuff if you can get the same result using querySelector. I don't get it
4th Aug 2020, 7:53 PM
Artur
Artur - avatar
+ 1
May be I don't understand something, idk
4th Aug 2020, 7:55 PM
Artur
Artur - avatar