# Muoversi da jQuery a javascript **Usare Javascript invece che jQuery** nei tuoi progetti può essere una mossa molto intelligente, non solo imparerai più a fondo le dinamiche del linguaggio e diventarai naturalmente più esperto, ma incrementerai notevolmente le performance del tuo sito. Se lavori in **grossi progetti e applicazioni** che richiedono tante linee di codice lavorare in puro Javascript è un po frustrante, in questo caso un mix di JQuery e Javascript è necessario. ## Tabella dei contenuti 1. [Eventi] (#eventi) 2. [Selettori] (#selettori) 3. [Modifica Attributi] (#modifica) 4. [Aggiungere o togliere una classe] (#addremove) 5. [Manipolazione del DOM] (#DOM) 6. [Transversing, muoversi nel DOM] (#transversing) 7. [AJAX] (#ajax) 8. [JSONP] (#jsonp) ## Eventi ```javascript // jQuery $(document).ready(function() { // code }) // javascript document.addEventListener('DOMContentLoaded', function() { // code }) ``` ```javascript // jQuery $('a').click(function() { // code… }) // javascript [].forEach.call(document.querySelectorAll('a'), function(el) { el.addEventListener('click', function() { // code… }) }) ``` [torna all'indice ↑] (#TOC) ## Selettori ```javascript // jQuery var divs = $('div') // javascript var divs = document.querySelectorAll('div') ``` ```javascript // jQuery var newDiv = $('
') // javascript var newDiv = document.createElement('div') ``` [torna all'indice ↑] (#TOC) ## Modifica Attributi ```javascript // jQuery $('img').filter(':first').attr('alt', 'My image') // javascript document.querySelector('img').setAttribute('alt', 'My image') ``` [torna all'indice ↑] (#TOC) ### Aggiungere o togliere una classe ```javascript // jQuery newDiv.addClass('foo') // javascript newDiv.classList.add('foo') ``` ```javascript // jQuery newDiv.toggleClass('foo') // javascript newDiv.classList.toggle('foo') ``` [torna all'indice ↑] (#TOC) ## Manipolazione del DOM ```javascript // jQuery $('body').append($('')) // javascript document.body.appendChild(document.createElement('p')) ``` ```javascript // jQuery var clonedElement = $('#about').clone() // javascript var clonedElement = document.getElementById('about').cloneNode(true) ``` ```javascript // jQuery $('#wrap').empty() // javascript var wrap = document.getElementById('wrap') while(wrap.firstChild) wrap.removeChild(wrap.firstChild) ``` [torna all'indice ↑] (#TOC) ## Transversing, muoversi nel DOM ```javascript // jQuery var parent = $('#about').parent() // javascript var parent = document.getElementById('about').parentNode ``` ```javascript // jQuery if($('#wrap').is(':empty')) // javascript if(!document.getElementById('wrap').hasChildNodes()) ``` ```javascript // jQuery var nextElement = $('#wrap').next() // javascript var nextElement = document.getElementById('wrap').nextSibling ``` [torna all'indice ↑] (#TOC) ## AJAX ### Richiesta GET ```javascript // jQuery $.get('//example.com', function (data) { // code }) // javascript var httpRequest = new XMLHttpRequest() httpRequest.onreadystatechange = function (data) { // code } httpRequest.open('GET', url) httpRequest.send() ``` ### Richiesta POST ```javascript // jQuery $.post('//example.com', { username: username }, function (data) { // code }) // javascript 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)) ``` [torna all'indice ↑] (#TOC) ### JSONP ```javascript // jQuery $.getJSON('//openexchangerates.org/latest.json?callback=?', function (data) { // code }) // javascript function success(data) { // code } var scr = document.createElement('script') scr.src = '//openexchangerates.org/latest.json?callback=formatCurrency' document.body.appendChild(scr) ``` [torna all'indice ↑] (#TOC)