Skip to content

Instantly share code, notes, and snippets.

@liamcurry
Created May 4, 2012 19:56
Show Gist options
  • Save liamcurry/2597326 to your computer and use it in GitHub Desktop.
Save liamcurry/2597326 to your computer and use it in GitHub Desktop.
Vanilla JS vs jQuery

Moving from jQuery

Events

// jQuery
$(document).ready(function() {
  // code
})

// Vanilla
document.addEventListener('DOMContentLoaded', function() {
  // code
})
// jQuery
$('a').click(function() {
  // code…
})

// Vanilla
[].forEach.call(document.querySelectorAll('a'), function(el) {
  el.addEventListener('click', function() {
    // code…
  })
})

Selectors

// jQuery
var divs = $('div')

// Vanilla
var divs = document.querySelectorAll('div')
// jQuery
var newDiv = $('<div/>')

// Vanilla
var newDiv = document.createElement('div')

Attributes

// jQuery
$('img').filter(':first').attr('alt', 'My image')

// Vanilla
document.querySelector('img').setAttribute('alt', 'My image')

Classes

// jQuery
newDiv.addClass('foo')

// Vanilla
newDiv.classList.add('foo')
// jQuery
newDiv.toggleClass('foo')

// Vanilla
newDiv.classList.toggle('foo')

Manipulation

// jQuery
$('body').append($('<p/>'))

// Vanilla
document.body.appendChild(document.createElement('p'))
// jQuery
var clonedElement = $('#about').clone()

// Vanilla
var clonedElement = document.getElementById('about').cloneNode(true)
// jQuery
$('#wrap').empty()

// Vanilla
var wrap = document.getElementById('wrap')
while(wrap.firstChild) wrap.removeChild(wrap.firstChild)

Transversing

// jQuery
var parent = $('#about').parent()

// Vanilla
var parent = document.getElementById('about').parentNode
// jQuery
if($('#wrap').is(':empty'))

// Vanilla
if(!document.getElementById('wrap').hasChildNodes())
// jQuery
var nextElement = $('#wrap').next()

// Vanilla
var nextElement = document.getElementById('wrap').nextSibling

AJAX

GET

// jQuery
$.get('//example.com', function (data) {
  // code
})

// Vanilla
var httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = function (data) {
  // code
}
httpRequest.open('GET', url)
httpRequest.send()

POST

// jQuery
$.post('//example.com', { username: username }, function (data) {
  // code
})

// Vanilla
var httpRequest = new XMLHttpRequest()
httpRequest.onreadystatechange = function (data) {
  // code
}
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')
httpRequest.open('POST', url)
httpRequest.send('username=' + encodeURIComponent(username))

JSONP

// jQuery
$.getJSON('//openexchangerates.org/latest.json?callback=?', function (data) {
  // code
})

// Vanilla
function success(data) {
  // code
}
var scr = document.createElement('script')
scr.src = '//openexchangerates.org/latest.json?callback=formatCurrency'
document.body.appendChild(scr)
@Taymindis
Copy link

why am i getting this error document.querySelectorAll('div:not(div div)'); ?

but jquery is working fine $('div:not(div div)');

@Elyoukey
Copy link

i think some selectors are not browser natives, and there is a jquery processing going on top of the default queryselector.

@hribeirosantana
Copy link

JavaScript reads almost like english, it is beautiful, I love it. The other one, although shorter sometimes, feels annoying and ugly to understand.

@WebTerminator
Copy link

Amazing, I am building my first project in vanilla JS. This is really helpful. Thank you

@holic158
Copy link

holic158 commented Sep 3, 2017

Thanks. Really.

@SassanKermani
Copy link

vanilla js for life

@Ya-Za
Copy link

Ya-Za commented Aug 4, 2018

Good job :-)
Instead of:

// Vanilla
[].forEach.call(document.querySelectorAll('a'), function(el) {
  el.addEventListener('click', function() {
    // code…
  })
})

You can write:

// Vanilla
document.querySelectorAll('a').forEach(el => {
  el.addEventListener('click', function() {
    // code…
  });
});

@Ya-Za
Copy link

Ya-Za commented Aug 4, 2018

Instead of:

// jQuery
$('img').filter(':first').attr('alt', 'My image')

You can write:

// jQuery
$('img:first').attr('alt', 'My image')

or:

// jQuery
$('img').first().attr('alt', 'My image')

or more general:

// jQuery
$('img').eq(0).attr('alt', 'My image')

@Ya-Za
Copy link

Ya-Za commented Aug 4, 2018

Instead of:

// Vanilla
document.body.appendChild(document.createElement('p'))

You can write:

// Vanilla
document.body.append(document.createElement('p'))

@Ya-Za
Copy link

Ya-Za commented Aug 4, 2018

Instead of:

// Vanilla
var wrap = document.getElementById('wrap')
while(wrap.firstChild) wrap.removeChild(wrap.firstChild)

You can write:

// Vanilla
document.getElementById('wrap').innerHTML = '';

@Ya-Za
Copy link

Ya-Za commented Aug 4, 2018

Instead of:

// Vanilla
var httpRequest = new XMLHttpRequest()

You can write!

// Vanilla
const xhr = new XMLHttpRequest()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment