Replace jQuery methods with Vanilla Javascript
- Select something
- Select something within a parent
- Add class
- Remove class
- Toggle class
- Get attribute
- Set attribute
- Get data attribute
- Set data attribute
- Get Text
- Get HTML
- Insert HTML string
- Insert text string
- Show element
- Hide element
- Show element with transition
- Hide element with transition
- Loop through Array
- Loop through Object
- Add Event Listener
- Ajax request
Selecting element by class name. You can also use to select by tag or identifier:
// jQuery
$('.class-name');
// ES6
document.querySelector('.class-name'); // single
document.querySelectorAll('.class-name'); // multiple
This example is similar to the one above: with an additional selection throught a parent element:
// jQuery
parent.find('.class-name');
// ES6
parent.querySelector('.class-name'); // single
parent.querySelectorAll('.class-name'); // multiple
Adding a class into an element:
// jQuery
element.addClass('.class-name');
// ES6
element.classList.add('.class-name');
Removing a class from an element:
// jQuery
element.removeClass('.class-name');
// ES6
element.classList.remove('.class-name');
// jQuery
element.toggleClass('.class-name');
// ES6
element.classList.toggle('.class-name');
Get atrribute value from an element:
// jQuery
element.attr('href');
// ES6
element.getAttribute('href');
Set value into an attribute from an element:
// jQuery
element.attr('href', 'https://www.google.com');
// ES6
element.setAttribute('href', 'https://www.google.com');
Get data atrribute value from an element:
// jQuery
element.data('id');
// ES6
element.getAttribute('data-id');
element.dataset.id;
Set value into data attribute from an element:
// jQuery
element.data('id');
// ES6
element.setAttribute('data-id', 1);
element.dataset.id = 1;
Getting text from an element:
// jQuery
element.text();
// ES6
element.textContent;
Getting HTML from an element:
// jQuery
element.html();
// ES6
element.innerHTML;
Adding HTML into an element:
// jQuery
element.html('<a href="https://www.gogle.com">Google</a>');
// ES6
element.innerHTML = '<a href="https://www.gogle.com">Google</a>';
Adding text into an element:
// jQuery
element.text('Your text here');
// ES6
element.innerText = 'Your text here';
// jQuery
element.show();
// ES6
element.style.display = 'block';
// jQuery
element.hide();
// ES6
element.style.display = 'none';
// jQuery
element.fadeIn();
// ES6
function fadeIn (element) {
element.classList.add('show');
element.classList.remove('hide');
}
Using vanilla javascript, require extra css:
.show {
transition: opacity 400ms;
}
.hide {
opacity: 0;
}
// jQuery
element.fadeOut();
// ES6
function fadeOut (element) {
element.classList.add('hide');
element.classList.remove('show');
}
Using vanilla javascript, require extra css:
.show {
opacity: 1;
}
.hide {
opacity: 0;
transition: opacity 400ms;
}
// jQuery
array.each((item), function {
// ...
});
// ES6
array.forEach((item) => {
// ...
});
// jQuery
object.each((key, value), function {
// ...
});
// ES6
for (const key in object) {
console.log(key, object[key]);
}
// or
for (const [key, value] of Object.entries(object)) {
console.log(key, value);
}
// jQuery
element.on( 'click', function(event) {
// ...
});
// ES6
element.addEventListener('click', (event) => {
// ...
});
// jQuery
$.ajax({
url: 'https://example.com',
method: 'post',
dataType: 'json',
contentType: 'application/json',
data: JSON.stringify(data),
success: function(response){
// ...
},
error: function(error){
// ...
}
});
// ES6
fetch( 'https://example.com', {
method: 'post',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(data)
})
.then(response => {
// ...
})
.catch(error => {
// ...
});
Please, read CONTRIBUTION.md file
@laisfrigerio |
---|
This project is licensed under the MIT License - see the LICENSE.md file for details