| jQuery | Vanilla |
|---|---|
| ```javascript $(document).ready(function() { // code }) ``` | ```javascript document.addEventListener('DOMContentLoaded', function() { // code }) ``` |
| ```javascript var divs = $('div') ``` | ```javascript var divs = document.querySelectorAll('div') ``` |
|
```javascript
var newDiv = $(' ')
```
|
```javascript var newDiv = document.createElement('div') ``` |
| ```javascript newDiv.addClass('foo') ``` | ```javascript newDiv.classList.add('foo') ``` |
| ```javascript newDiv.toggleClass('foo') ``` | ```javascript newDiv.classList.toggle('foo') ``` |
| ```javascript $('a').click(function() { // code… }) ``` | ```javascript [].forEach.call(document.querySelectorAll('a'), function(el) { el.addEventListener('click', function() { // code… }) }) ``` |
|
```javascript
$('body').append($(' ')) ``` |
```javascript document.body.appendChild(document.createElement('p')) ``` |
| ```javascript $('img').filter(':first').attr('alt', 'My image') ``` | ```javascript document.querySelector('img').setAttribute('alt', 'My image') ``` |
| ```javascript var parent = $('#about').parent() ``` | ```javascript var parent = document.getElementById('about').parentNode ``` |
| ```javascript var clonedElement = $('#about').clone() ``` | ```javascript var clonedElement = document.getElementById('about').cloneNode(true) ``` |
| ```javascript $('#wrap').empty() ``` | ```javascript var wrap = document.getElementById('wrap') while(wrap.firstChild) wrap.removeChild(wrap.firstChild) ``` |
| ```javascript if($('#wrap').is(':empty')) ``` | ```javascript if(!document.getElementById('wrap').hasChildNodes()) ``` |
| ```javascript var nextElement = $('#wrap').next() ``` | ```javascript var nextElement = document.getElementById('wrap').nextSibling ``` |
-
-
Save pydawan/0a462ff0516ba6a557f16ffc02dfb1d9 to your computer and use it in GitHub Desktop.
Vanilla JS equivalents of jQuery methods
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment