JavaScript DOM - Part 5 - Get Elements By ClassName [video + article] Tharun Shiv on May 21, 2020 This is going to be a multi-part Video + article tutorial series on JavaScript DOM. You're reading Part 5 You can read Part 4 here: ... Read full post Submit Preview Dismiss Collapse Expand umamaheswari.v umamaheswari.v umamaheswari.v Follow Professor Location Bangalore Work H.o.d at BCC Joined May 20, 2020 • May 22 '20 Dropdown menu Copy link Hide Good article, looking forward for more.. :) Collapse Expand venkat anirudh venkat anirudh venkat anirudh Follow Joined May 15, 2020 • May 22 '20 Dropdown menu Copy link Hide What if there is only one element with that class name, would it still return an HTML Collection? Good article, btw 🔥 Collapse Expand Habdul Hazeez Habdul Hazeez Habdul Hazeez Follow I am a writer interested in Web Development, Computer Security, and Artificial Intelligence. Location Nigeria Education B.Sc. Computer Science Work Web Developer and Technical Writer Joined Feb 25, 2019 • May 22 '20 • Edited on May 22 • Edited Dropdown menu Copy link Hide Yes, document.getElementsByClassName would still return an HTMLcollection if there is only one element with that class name. Take the following code snippet. <form class="myForm"> <input type="text" placeholder="Name" /> <input type="email" placeholder="Email Address" /> <input type="submit" value="Subscribe" /> </form> let myForm = document.getElementsByClassName('myForm'); console.log(myForm); The console will log: HTMLCollection { 0: form.myForm, length: 1 } If you have just a single element with a unique class name, you should prefer document.querySelector over document.getElementsByClassName. It's syntax will be: let myForm = document.querySelector('.myForm'); // note the "." before the class name console.log(myForm); The output in the console will be: <form class="myForm"> You can also attach an HTML ID to it and use document.getElementByID. @praveenreddy1798 @umamaheswari.v Collapse Expand venkat anirudh venkat anirudh venkat anirudh Follow Joined May 15, 2020 • May 22 '20 Dropdown menu Copy link Hide Got it, thank you Habdul 😊 Habdul Hazeez Habdul Hazeez Habdul Hazeez Follow I am a writer interested in Web Development, Computer Security, and Artificial Intelligence. Location Nigeria Education B.Sc. Computer Science Work Web Developer and Technical Writer Joined Feb 25, 2019 • May 22 '20 • Edited on May 22 • Edited Dropdown menu Copy link Hide You are welcome. @praveenreddy1798 @umamaheswari.v Collapse Expand praveenreddy1798 praveenreddy1798 praveenreddy1798 Follow A programmer by need Location Bangalore Work Full stack developer Joined May 19, 2020 • May 22 '20 Dropdown menu Copy link Hide Thank you @Habdul 😊 Collapse Expand Tharun Shiv Tharun Shiv Tharun Shiv Follow Site Reliability Engineer | Blogger | Podcasts | Youtube @ developertharun Email asktharun@gmail.com Location Bangalore Education Computer Science Engineering Work Site Reliability Engineer at PhonePe Joined May 3, 2020 • May 22 '20 Dropdown menu Copy link Hide That's a great answer in detail. thank you🙂 Collapse Expand Jaya chandrika reddy Jaya chandrika reddy Jaya chandrika reddy Follow A programmer who tries to teach programming knowledge with fun Location Bengaluru Education B.E Work Software engineer at Commscope Joined May 4, 2020 • May 22 '20 Dropdown menu Copy link Hide Great Article, will we also get to know about querySelector in this series? Collapse Expand Tharun Shiv Tharun Shiv Tharun Shiv Follow Site Reliability Engineer | Blogger | Podcasts | Youtube @ developertharun Email asktharun@gmail.com Location Bangalore Education Computer Science Engineering Work Site Reliability Engineer at PhonePe Joined May 3, 2020 • May 22 '20 Dropdown menu Copy link Hide Yes, they're coming up😊 Collapse Expand Temitope A Ogunbiyi Temitope A Ogunbiyi Temitope A Ogunbiyi Follow Joined May 22, 2020 • May 22 '20 Dropdown menu Copy link Hide Great lesson Tharun Collapse Expand Tharun Shiv Tharun Shiv Tharun Shiv Follow Site Reliability Engineer | Blogger | Podcasts | Youtube @ developertharun Email asktharun@gmail.com Location Bangalore Education Computer Science Engineering Work Site Reliability Engineer at PhonePe Joined May 3, 2020 • May 22 '20 Dropdown menu Copy link Hide Thank you 🙂 Collapse Expand Mohamed Dahir Mohamed Dahir Mohamed Dahir Follow Location London, UK Education B.Sc. CS @ Hormuud University Joined Jun 29, 2019 • May 22 '20 Dropdown menu Copy link Hide Do you plan on introducing querySelector and querySelectorAll for getting DOM elements? And maybe how to alias them to $ and $$ respectively? Collapse Expand Tharun Shiv Tharun Shiv Tharun Shiv Follow Site Reliability Engineer | Blogger | Podcasts | Youtube @ developertharun Email asktharun@gmail.com Location Bangalore Education Computer Science Engineering Work Site Reliability Engineer at PhonePe Joined May 3, 2020 • May 22 '20 Dropdown menu Copy link Hide Hi Mohamed, Yes, they are definitely included as a part of this series. 😊 Thank you for showing interest Code of Conduct • Report abuse
Good article, looking forward for more.. :)
What if there is only one element with that class name, would it still return an HTML Collection? Good article, btw 🔥
Yes,
document.getElementsByClassNamewould still return anHTMLcollectionif there is only one element with that class name.Take the following code snippet.
The console will log:
If you have just a single element with a unique class name, you should prefer
document.querySelectoroverdocument.getElementsByClassName.It's syntax will be:
The output in the console will be:
You can also attach an HTML ID to it and use
document.getElementByID.@praveenreddy1798
@umamaheswari.v
Got it, thank you Habdul 😊
You are welcome.
@praveenreddy1798
@umamaheswari.v
Thank you @Habdul 😊
That's a great answer in detail. thank you🙂
Great Article, will we also get to know about querySelector in this series?
Yes, they're coming up😊
Great lesson Tharun
Thank you 🙂
Do you plan on introducing
querySelectorandquerySelectorAllfor getting DOM elements? And maybe how to alias them to$and$$respectively?Hi Mohamed,
Yes, they are definitely included as a part of this series. 😊 Thank you for showing interest