Skip to content

Instantly share code, notes, and snippets.

@ahmadmilzam
Forked from magicznyleszek/javascript-vs-jquery.md
Last active September 4, 2015 09:31
Show Gist options
  • Select an option

  • Save ahmadmilzam/064afa5f153616fb871b to your computer and use it in GitHub Desktop.

Select an option

Save ahmadmilzam/064afa5f153616fb871b to your computer and use it in GitHub Desktop.
Vanilla JavaScript

Vanilla JavaScript

Some vanilla equivalents to jQuery methods.

DOM Selectors

jQuery:

var target = $('.some. classes');

JavaScript

var target = document.querySelectorAll('.some. classes');

To be precise, there are five different ways for it:

  1. document.querySelectorAll(selector) -- all matching nodes
  2. document.querySelector(selector) -- the first matching node
  3. document.getElementById(idname) -- a single node by id
  4. document.getElementsByTagName(tagname) -- all nodes by html tag
  5. document.getElementsByClassName(class) -- all nodes by class

You can link those to find descendants:

var parent = document.getElementById('distinct-id');
var kids = parent.getElementsByClassName('my-class');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment