#1 India's Top IT Training Institute
×

About Uncodemy

Know more about who we are and what we stand for.

About Us

About Us

Discover our mission to equip individuals with essential IT skills and industry expertise.

Read More →
UnCodeMy Gallery

UnCodeMy Gallery

Explore highlights from our IT training sessions and success stories.

Read More →
Mentors

Mentors

Meet the industry experts who train and guide you through your course.

Read More →
Free Resources

Free Resources

Free tutorials, interview questions, tools and calculators to help you learn.

Explore →
×

Free Resources

Learn, practice and plan your career — bilkul free.

Tutorials

Tutorials

Step-by-step written tutorials to learn any technology at your own pace.

Read More →
Free Counselling

Free Counselling

Book a free video counselling session and pick the right course for you.

Read More →
Online Compiler

Online Compiler

Write, run and test your code online — no setup required.

Read More →
jQuery

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 the jQuery function. $('p') and jQuery('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() or on().

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').value is undefined; use $('#email').val().
  • Inside a handler, this is a plain DOM element, so this.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 this as $(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.

Explore Course