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 = $('.page-content');

JavaScript:

var target = document.querySelectorAll('.page-content');

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');

Next element

jQuery:

var nextElement = $('.page-content').next();

JavaScript:

var nextElement = document.querySelector('.page-content').nextSibling;

DOM manipulation

Adding HTML

jQuery:

$('.page-content').append('<p>Some more text.</p>');

JavaScript:

var something = document.createElement('p');
something.appendChild(document.createTextNode('Some more text.'));
document.querySelector('.page-content').appendChild(something);

Clear HTML element content

jQuery:

$('.page-content').empty();

JavaScript:

document.querySelector('.page-contentt').innerHTML = null;

Check if element is empty

jQuery:

if ($('.page-content').is(':empty'))

JavaScript:

if (!document.getElementById('.page-content').hasChildNodes());

Remove HTML element

jQuery:

$('.page-content').remove();

JavaScript:

var something = document.querySelector('.unwanted-stuff');
something.parentNode.removeChild(something);

HTML attributes

Managing Classes

jQuery:

$('.list-element').addClass('active');
$('.list-element').removeClass('active');
$('.list-element').toggleClass('active');

JavaScript:

document.querySelector('.list-element').classList.add('active');
document.querySelector('.list-element').classList.remove('active');
document.querySelector('.list-element').classList.toggle('active');

Managing attributes

jQuery:

$('.home-link').attr('href');
$('.home-link').attr('href', 'http://corpo.org');

JavaScript:

document.querySelector('.home-link').getAttribute('href');
document.querySelector('.home-link').setAttribute('href', 'http://corpo.org');

Events

Click

jQuery:

$('.clickable').on('click', function(e) {
    console.log('You clicked ' + e.target);
    e.preventDefault();
});

JavaScript:

document.querySelectorAll('.clickable').addEventListener('click', function(e) {
    console.log('You clicked ' + e.target);
    e.preventDefault();
});

You can also use one of these simple GlobalEventHandlers events:

  1. element.onclick = functionRef; -- when clicked, after the mousedown and mouseup
  2. element.oninput = functionRef; -- when input element value changes
  3. window.onload = funcRef; -- after all DOM elements and images are loaded
  4. element.onscroll = functionRef; -- when content of the element is scrolled
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment