• 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:

  • Get the computed style of an element
  • Get all of an element’s parent elements up the DOM tree
  • Replace a Class of an Element
  • Get the Children of an Element

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