jQuery Syntax
jQuery syntax follows one basic pattern: $(selector).action(). The dollar sign calls the jQuery function, the selector picks elements from the page using CSS selector rules, and the action is a method that does something to every element that matched. Learning this pattern, and a few rules around it, lets you read almost any jQuery code.
Breaking down $(selector).action()
$is a short alias for thejQueryfunction.$('p')andjQuery('p')are identical.- The selector is a string such as
'p','#menu','.card'or'ul li:first-child'. - The action is a jQuery method like
hide(),addClass(),text()oron().
The call to $() returns a jQuery object: an array-like wrapper around all matched elements. Methods on it apply to each element, so $('.card').addClass('shadow') updates every card without a loop.
Chaining and getters versus setters
Most jQuery methods return the same jQuery object, so you can chain calls one after another. Many methods also act as both getter and setter, depending on whether you pass a value.
$(function () {
// setter: applies to every matched element, returns jQuery object
$('.price')
.addClass('highlight')
.css('font-weight', 'bold')
.attr('title', 'Including tax');
// getters: .val() and .attr() read only the FIRST match
const email = $('#email').val();
const firstTitle = $('.price').attr('title');
// .text() is different: it joins the text of ALL matches
const allPrices = $('.price').text();
// event handling and `this`
$('.faq-question').on('click', function () {
$(this).next('.faq-answer').slideToggle(150);
$(this).toggleClass('open');
});
});
The getter rule is worth remembering. Called without a value, .val(), .attr(), .css() and .html() read from the first matched element only, while their setter versions change every match. .text() is the odd one out: as a getter it combines the text of all matched elements into one string. If you need each element's value separately, loop with .each() or use .map().
Wrapping code in the ready handler
jQuery code usually sits inside $(function () { ... }). This runs your code once the HTML has been parsed into the DOM, so selectors can find their elements. Without it, a script placed in the <head> would select nothing, and jQuery would quietly do nothing instead of throwing an error.
jQuery objects are not DOM elements
A frequent mix-up is calling plain DOM properties on a jQuery object, or jQuery methods on a DOM element:
$('#email').valueisundefined; use$('#email').val().- Inside a handler,
thisis a plain DOM element, sothis.hide()fails. Wrap it:$(this).hide(). - To get the raw element from a jQuery object, use
$('#email')[0]or.get(0).
Another quiet failure: jQuery does not complain when a selector matches nothing. If $('#emial').hide() has a typo, nothing happens and no error appears. Check .length when debugging; a value of 0 means no elements matched.
When $ is already taken
Other libraries sometimes use $ too. jQuery.noConflict() releases the alias so you can keep using the full name jQuery. A common pattern is to receive $ safely as a parameter of the ready handler: jQuery(function ($) { ... }), which lets you keep using $ inside even when it means something else outside.
Main points
- Every statement follows
$(selector).action(). - Setters affect all matches and return the jQuery object for chaining.
- Wrap
thisas$(this)to use jQuery methods on it.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.