on(event, callback) | Add event listener to selected elementents. | $('#button').on('click', () => alert('Click!')); |
off(event, callback) | Remove event listener. | $('#button').off('click', callback); |
addClass(className) | Add CSS class. | $('.box').addClass('highlight'); |
removeClass(className) | Remove CSS class. | $('.box').removeClass('highlight'); |
toggle(className) | Toggle class or visibility. | $('.box').toggle('active'); |
hasClass(className) | Check if the elementent has the specified class. | $('.box').hasClass('highlight'); |
text(value) | Get/set textual content. | $('#title').text('New text'); |
html(value) | Get/set HTML content. | $('#content').html('Hello'); |
val(value) | Get/set input value. | $('#name').val('Anna'); |
attr(name, value) | Get/set attribute. | $('img').attr('alt', 'Image description'); |
removeAttr(name) | Remove attribute. | $('img').removeAttr('alt'); |
prop(name, value) | Get/set property. | $('#check').prop('checked', true); |
data(name, value) | Handle data attribute. | $('.element').data('type', 'info'); |
show(display) | Show elementent. | $('#box').show(); |
hide() | Hide elementent. | $('#box').hide(); |
fadeIn(duration) | Fade in with animation. | $('#info').fadeIn(300); |
fadeOut(duration) | Fade out with animation. | $('#info').fadeOut(300); |
slideToggle(duration) | Toggle slide animation. | $('#menu').slideToggle(300); |
append(content) | Append content to the end. | $('#list').append(' Item'); |
prepend(content) | Prepend content to the beginning. | $('#list').prepend(' First'); |
remove() | Remove elementent from the DOM. | $('.remove').remove(); |
empty() | Empty the elementent content. | $('#box').empty(); |
detach() | Detach elementent from DOM for reuse. | let element = $('#box').detach(); |
clone() | Clone elementent. | let new = $('#button').clone(); |
siblings() | Get sibling elementents. | $('#element').siblings(); |
parents() | Get parent elementents. | $('#element').parents(); |
next() | Next sibling elementent. | $('#element').next(); |
prev() | Previous sibling elementent. | $('#element').prev(); |
first() | Get first elementent. | $('.element').first(); |
last() | Get last elementent. | $('.element').last(); |
filter(callback) | Filter elementents based on condition. | $('li').filter((el, i) => i % 2 === 0); |
not(selector) | Exclude elementents matching selector. | $('div').not('.active'); |
eq(index) | Select elementent by index. | $('li').eq(2); |
find(selector) | Find child elementents. | $('#nav').find('a'); |
closest(selector) | Closest matching parent. | $('input').closest('form'); |
each(callback) | Iterate over each elementent. | $('p').each((el, i) => console.log(el)); |
is(selector) | Check elementent matching selector. | $('#check').is(':checked'); |
focus() | Set focus. | $('#name').focus(); |
blur() | Remove focus. | $('#name').blur(); |
scrollTop(value) | Set vertical scroll. | $(window).scrollTop(100); |
scrollLeft(value) | Set horizontal scroll. | $(window).scrollLeft(50); |
height(value) | Get/set height. | $('#box').height(200); |
width(value) | Get/set width. | $('#box').width(300); |
serialize() | Serialize form data to URL encoding. | let data = $('form').serialize(); |
wrap(elementent) | Wrap the selected elementent with new HTML. | $('.box').wrap(elementent); |
unwrap(elementent) | Remove the parent but keep the child. | $('.box').unwrap(); |
toggleClass(value) | Toggle the given class on the elementent. | $('.box').toggleClass('active'); |
css(drop, value) | Get/set CSS styles. |
$('.box').css({
color: 'white',
backgroundColor: 'black',
padding: '10px'
});
const color = $('.box').css('color');
console.log('Text color:', color);
|
cssVar(value) | Get/set CSS variables. |
Querying a single variable:
const color = $(':root').cssVar('--main-color');
Setting a single variable:
$('body').cssVar('--main-color', '#00f');
To set multiple variables at once:
$('body').cssVar({
'--main-color': '#0f0',
'--font-size': '18px'
});
|
animate({...}, time); | Animate numeric CSS properties over time. |
The width of the .box increases to 300px and its transparency decreases to 0.5 in 400 ms:
$('.box').animate({
width: '300px',
opacity: 0.5
}, 400);
|
serializeArray() | Return form fields as array of JS objects. |
const data = $('form').serializeArray();
console.log(data);
// [
// { name: 'name', value: 'John' },
// { name: 'email', value: 'joe@example.com' },
// ...
// ]
|
serializeObject() | Return form fields as key-value object. |
const data = $('form').serializeObject();
console.log(data);
// {
// name: "John",
// email: "joe@example.com",
// tags: ["html", "css"]
// }
can be used:
JSON.stringify($('form').serializeObject())
|
colorAnimate(property = 'backgroundColor', toColor = '#ffffff', duration = 400) | Animate color on given CSS property (e.g. backgroundColor). |
Changes the backgroundColor to the given color in 600ms:
$('.box').colorAnimate('backgroundColor', '#00ffcc', 600);
Supported formats:
#hex, #rrggbb
rgb(r,g,b)
CSS colors (e.g. "red", "orange") also work because the RGB will be extracted from the canvas
|
toggleAttr(name) | Toggle given attribute on elementents. |
If the button is disabled, it removes it — if not, it adds it:
$('#myButton').toggleAttr('disabled');
|
hasAttr(name) | Check if any elementent has the given attribute. |
if ($('#myButton').hasAttr('disabled')) {
console.log('The button is disabled.');
}
|
scrollTo(duration = 500) | Smoothly scroll to elementent over duration. |
This scrolls to the #article element in 700ms:
$('#article').scrollTo(700);
They also work together:
$('#article')
.toggleAttr('aria-expanded')
.scrollTo(500);
|
ajax({...}) | Generic AJAX request. |
$.ajax({
url: '/api',
method: 'POST',
data: { name: 'Bob' }
}); |
get(url, params) | Simple GET request. | $.get('/data', { id: 1 }); |
post(url, data) | Simple POST request. | $.post('url', {
key1: 'value1',
key2: 'value2'
})
.then(data => {
console.log('Server response:', data);
})
.catch(error => {
console.error('AJAX error:', error);
}); |
ready(callback) | Run function on DOM ready. | $.ready(() => console.log('Ready!')); |
reload(force) | Reload page. | $.reload(); |
redirect(url) | Redirect to new URL. | $.redirect('/login'); |