Skip to content

Instantly share code, notes, and snippets.

@KuroNoDev
Forked from liamcurry/gist:2597326
Last active October 6, 2016 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KuroNoDev/f41b6dc6ee66392a9058b877ebc81d7a to your computer and use it in GitHub Desktop.
Save KuroNoDev/f41b6dc6ee66392a9058b877ebc81d7a to your computer and use it in GitHub Desktop.
Vanilla JS vs jQuery

Events

// Vanilla
document.addEventListener('DOMContentLoaded', function() {
  // code
})

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

// jQuery
$('a').click(function() {
  // code…
})

Selectors

// Vanilla
var divs = document.querySelectorAll('div')

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

// jQuery
var newDiv = $('<div/>')

Attributes

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

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

Classes

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

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

// jQuery
newDiv.toggleClass('foo')

Manipulation

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

// jQuery
$('body').append($('<p/>'))
// Vanilla
var clonedElement = document.getElementById('about').cloneNode(true)

// jQuery
var clonedElement = $('#about').clone()
// Vanilla
var wrap = document.getElementById('wrap')
while(wrap.firstChild) wrap.removeChild(wrap.firstChild)

// jQuery
$('#wrap').empty()

Transversing

// Vanilla
var parent = document.getElementById('about').parentNode

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

// jQuery
if($('#wrap').is(':empty'))
// Vanilla
var nextElement = document.getElementById('wrap').nextSibling

// jQuery
var nextElement = $('#wrap').next()

AJAX

GET

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

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

POST

// 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))

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

JSONP

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

// jQuery
$.getJSON('//openexchangerates.org/latest.json?callback=?', function (data) {
  // code
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment