• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
JavaScriptSource

JavaScriptSource

Search 5,000+ Free JavaScript Snippets

  • Home
  • Browse Snippets
    • Addon
    • Ajax
    • Buttons
    • Cookies
    • CSS
    • Featured
    • Forms
    • Games
    • Generators
    • Image Effects
    • Math Related
    • Miscellaneous
    • Multimedia
    • Navigation
    • Page Details
    • Passwords
    • Text Effects
    • Time & Date
    • User Details
Home / CSS / Select an element or list of elements

Select an element or list of elements

Select an element or list of elements

Select an element or elements by given ID, class name, tag name, or CSS selector.

Select an element by given ID

<div id="hello" />

document.getElementById('hello');

Select elements by class name

Returns the list of elements that have hello class within a given element:

ele.getElementsByClassName('hello');

Select elements by tag name

Returns the list of span inside a given element:

ele.getElementsByTagName('span');

Select elements by CSS selector

Returns the list of elements that match a given selector:

ele.querySelectorAll('div.hello');

Returns the first element that match a given selector:

ele.querySelector('div.hello');

Source

https://htmldom.dev/select-an-element-or-list-of-elements/

CSS

Related Snippets:

  • Toggle a Class of an Element
  • Resize the width of a text box to fit its content automatically
  • Add a Class to an Element
  • Find all matching elements on a page with document.querySelectorAll()

Primary Sidebar

Popular Posts

Story Generator

IP Grabber – get a users IP address with JavaScript

Simple Calendar

Remove Ads

Astrological Calculator

Copyright © 2025 JavaScriptSource.com

  • About
  • Privacy Policy
  • FAQ
  • Jobs For Developers