JAVASCRIPT QUESTION | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 31

JAVASCRIPT QUESTION

Hi guys! What exactly does document.querySelector() and document.querySelectorAll() do? I've checked online but I'm yet to find a `dumbed-down` explanation of them. Can someone please explain with examples?

13th Aug 2020, 10:16 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
51 Answers
+ 49
There is no document.querySelectors it is querySelectorAll. querySelector selects the first element that matches a CSS selector. querySelectorAll returns a collection of all elements that matches a CSS selector. EDIT: Wow. I never expected this much upvotes. 😄 Thanks everyone
13th Aug 2020, 10:20 AM
Ore
Ore - avatar
+ 20
If you are known to css then it is a very easy task! document.querySelector("#haha") Is same as document.getElementById("haha") And document.querySelectorAll(".haha")[0] Is same aa document.getElementsByClassName("haha")[0]
13th Aug 2020, 10:22 AM
Namit Jain
Namit Jain - avatar
+ 14
Hey 👑Tchybooxuur! I created this small code to demonstrate the difference. https://code.sololearn.com/W0Gjy3PZbM6X
13th Aug 2020, 10:37 AM
Ore
Ore - avatar
+ 11
Ore It would help a great deal if you could explain what each line of your JS code does. Perhaps by way of commenting or by any other way you deem fit. You could totally do it whenever you are chanced.
13th Aug 2020, 10:50 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 7
Ore Thanks. Let me edit accordingly 👍🏼
13th Aug 2020, 10:24 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 7
Namit Jain Whoa! Thanks men 👍🏼
13th Aug 2020, 10:26 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 7
Thanks Ore Checking it out right away!
13th Aug 2020, 10:43 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 7
👑Tchybooxuur! Sorry about that. I did not write it with the intention that it will be read. I expected you to just check the output and see what is returned by the two functions. Anyway, I will try to simplify the code. Pardon me.
13th Aug 2020, 10:53 AM
Ore
Ore - avatar
+ 7
👑Tchybooxuur! You can check it now
13th Aug 2020, 11:00 AM
Ore
Ore - avatar
+ 7
Ore maybe due to many friends 😂😅👦 Cuz he is a very friendly guy
13th Aug 2020, 11:14 AM
Namit Jain
Namit Jain - avatar
+ 7
That's what's up Ore I'm a very friendly guy! ☺️☺️☺️ Thanks Namit Jain 👌
13th Aug 2020, 11:20 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 6
Namit Jain Yeah. Maybe
13th Aug 2020, 11:20 AM
Ore
Ore - avatar
+ 6
Kode Krasher Haha. A champion for writing an unreadable code that the OP could not even understand? I feel ashamed rn
13th Aug 2020, 11:34 AM
Ore
Ore - avatar
+ 6
Ore 😂 That's entirely the fault of the OP. You did noble... 👍🏼
13th Aug 2020, 12:16 PM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 6
Kode Krasher I checked his profile and he's helped virtually everyone on sololearn with a web related question. He's that hot 🔥 upstairs! 😅😅😅
13th Aug 2020, 1:30 PM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 6
Kode Krasher: Very well said... 👍🏼
13th Aug 2020, 2:27 PM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar
+ 6
13th Aug 2020, 2:41 PM
Ore
Ore - avatar
+ 6
Jorat For the most part you are right except that `querySelectorAll` returns a NodeList not an array. A NodeList is also iterable like an array but does not have the array methods like reduce, filter.
13th Aug 2020, 8:32 PM
Ore
Ore - avatar
14th Aug 2020, 4:41 AM
Gordon
Gordon - avatar
+ 6
junayeD  ●_° Thanks a lot. You did a great job simplifying things. I have a further question not necessarily related to the above question. How would you write your function so that on clicking the button multiple times, it toggles between the new value and the initial value? Like if I press the button once, background turns red and when I press again, it goes back to white like so.
14th Aug 2020, 9:31 AM
👑 Tchybooxuur!
👑 Tchybooxuur! - avatar