jQuery Syntax
Learn the fundamental jQuery syntax pattern — how selectors work, what actions do, method chaining, the this keyword, and how to write clean professional jQuery code.
In This Lesson
- Basic Syntax
- The $ Symbol
- jQuery Selectors
- jQuery Actions
- Method Chaining
- The this Keyword
- Document Ready
- jQuery vs JavaScript
- Best Practices
- Try It Yourself
- FAQ
Basic jQuery Syntax
All jQuery code follows one simple pattern. Once you understand this pattern, you can read and write any jQuery statement:
This three-part pattern is the foundation of everything in jQuery. Every jQuery statement you will ever write follows exactly this structure.
// Hide all paragraphs $("p").hide(); // Change color of element with class 'highlight' $(".highlight").css("color", "red"); // Add click event to a button $("#myButton").click(function() { alert("Button was clicked!"); });
The $ Symbol
The $ is simply a shorthand alias for the word jQuery. Both are completely identical — you can use either one and get the exact same result.
// Using $ shorthand (standard practice) $("#myId").hide(); // Using full jQuery keyword (same result) jQuery("#myId").hide(); // Both lines do exactly the same thing
The $ is used because it is shorter, faster to type, and is the universal industry convention. You will almost always see $ in real jQuery code.
jQuery Selectors
Selectors are patterns that tell jQuery which HTML elements to find. jQuery uses the exact same CSS selector syntax you already know — so if you know CSS, you already know jQuery selectors.
Element, ID, and Class Selectors
// Element selector — selects ALL paragraphs $("p") // ID selector — selects ONE element with that id $("#myId") // Class selector — selects ALL elements with that class $(".myClass") // Multiple selectors — select all three at once $("p, div, span")
Complex Selectors
jQuery Actions
Actions are the jQuery methods you call on selected elements. They are grouped into four main categories:
Effect Methods — Show, hide, animate
$("p").hide(); // Hide all paragraphs $("p").show(); // Show all paragraphs $("p").toggle(); // Toggle hide/show $("p").fadeOut(); // Fade out smoothly $("p").slideDown(); // Slide down animation
DOM Methods — Read and change content
$("p").text("New text"); // Set text $("p").html("<b>Bold</b>"); // Set HTML $("p").append("Added text"); // Add at end $("p").remove(); // Remove element
CSS Methods — Style elements
$("p").css("color", "red"); // Set CSS property $("p").addClass("highlight"); // Add class $("p").removeClass("highlight"); // Remove class $("p").toggleClass("highlight"); // Toggle class
Event Methods — Handle interactions
$("button").click(function() {}); // Click event $("input").change(function() {}); // Change event $("p").hover(function() {}); // Hover event $("form").submit(function() {}); // Submit event
Method Chaining
jQuery allows you to chain multiple methods on the same selected element in a single statement. Instead of writing the selector multiple times, you write it once and keep adding . followed by the next method.
$("#box").hide(); $("#box").fadeIn(); $("#box").css("color","red"); $("#box").addClass("active");
$("#box")
.hide()
.fadeIn()
.css("color", "red")
.addClass("active");
The this Keyword
Inside a jQuery event handler, this refers to the specific HTML element that triggered the event. Wrap it in $(this) to use jQuery methods on it.
$("p").click(function() { // 'this' = raw DOM element — use for native JS this.style.color = "red"; this.innerHTML = "Clicked!"; // '$(this)' = jQuery object — use for jQuery methods $(this).css("color", "red"); $(this).hide(); $(this).addClass("clicked"); });
Document Ready
Always wrap your jQuery code inside a document ready function. This ensures your code only runs after the entire HTML page has been loaded and parsed. If you run jQuery before the DOM is ready your selectors will find nothing.
// Full syntax $(document).ready(function() { // All your jQuery code goes here $("p").click(function() { $(this).hide(); }); }); // Shorthand — most commonly used $(function() { // Same thing — shorter to write $("p").click(function() { $(this).hide(); }); });
jQuery vs Vanilla JavaScript
Here is a side-by-side comparison showing how much shorter jQuery makes common tasks:
Best Practices
Cache your selectors in variables
// Good — select once, use many times const $box = $("#myBox"); $box.hide(); $box.fadeIn(); $box.css("color", "red"); // Bad — searching DOM three separate times $("#myBox").hide(); $("#myBox").fadeIn(); $("#myBox").css("color", "red");
Use proper formatting for readability
// Good — readable and well formatted $(function() { $("button").click(function() { $(this) .hide() .fadeIn() .css("color", "red"); }); }); // Bad — impossible to read $(function(){$("button").click(function(){$(this).hide().fadeIn().css("color","red");});});
Try It Yourself
Practice everything from this lesson by building this interactive page:
Frequently Asked Questions
Common questions beginners ask about jQuery syntax:
The basic jQuery syntax is dollar sign open bracket selector close bracket dot action open bracket close bracket. The dollar sign gives you access to jQuery, the selector finds the HTML elements you want using CSS syntax, and the action is the jQuery method you want to run on those elements. Every jQuery statement follows this exact same three-part pattern.
The dollar sign is simply a shorthand alias for the jQuery function. Writing dollar sign open bracket selector close bracket and jQuery open bracket selector close bracket do exactly the same thing. The dollar sign is used because it is shorter, faster to type, and is the universal standard in the jQuery community. If another library also uses dollar sign you can call jQuery.noConflict() to resolve the conflict.
jQuery selectors use CSS selector syntax to find and select HTML elements on the page. Hash id selects by id, dot class selects all elements with that class name, and just the tag name selects all elements of that type. You can also combine selectors for more specific matches such as p dot intro to select only paragraphs that also have the intro class.
Method chaining lets you call multiple jQuery methods on the same selected element in a single statement without repeating the selector. You write the selector once and chain additional methods using dots. This is both cleaner to read and better for performance because jQuery only searches the DOM once instead of multiple times for the same element.
Inside a jQuery event handler, this is the raw JavaScript DOM element that triggered the event. You can use it to access native JavaScript properties like this.style or this.value. Dollar sign this wraps that DOM element in a jQuery object so you can call jQuery methods on it like dot css or dot addClass or dot hide. Always use dollar sign this when you want to use jQuery methods on the current element.
Wrapping your jQuery code inside document ready ensures it only runs after the entire HTML page has finished loading. If you run jQuery code before the DOM is ready your selectors will find no elements because the HTML has not been parsed yet. The shorthand version is dollar sign open bracket function open bracket close bracket open bracket your code here close bracket close bracket.