๐Ÿง  Introduction to jQuery & jQuery Selectors ๐Ÿ”

Payal PorwalPayal Porwal
3 min read

๐Ÿง  Introduction to jQuery

๐Ÿ’ก What is jQuery?

jQuery is a fast, small, and powerful JavaScript library. It makes things like HTML document traversal, event handling, animation, and AJAX much easier with just a few lines of code.

Think of jQuery as a shortcut to writing JavaScript. Instead of writing 10 lines of JavaScript, you can often do the same thing in just 1 or 2 lines using jQuery!


๐ŸŽฏ Advantages of Using jQuery

  • โœ… Easy to learn and use.

  • โœ… Reduces the amount of JavaScript code.

  • โœ… Cross-browser compatibility (works in all major browsers).

  • โœ… Simplifies AJAX calls.

  • โœ… Built-in animation and effects.


๐Ÿ”ง jQuery Syntax

javascriptCopyEdit$(selector).action()
  • $: This is how you start writing jQuery.

  • selector: Selects the HTML element(s).

  • action(): What you want to do (like hide, show, change text, etc).

๐Ÿงธ Example:

javascriptCopyEdit$("#title").hide();

This hides the element with id="title".


๐Ÿ” jQuery Selectors

Selectors are used to select and manipulate HTML elements. Letโ€™s go through the most common ones with real-life examples.


๐Ÿ”ท 1. ID Selector (#id)

Used to select an element by its ID.

Syntax: $("#elementID")

โœ… Real-life Example 1:

htmlCopyEdit<h1 id="mainHeading">Welcome!</h1>
javascriptCopyEdit$("#mainHeading").css("color", "blue");

๐Ÿง  Changes the text color of the heading to blue.

โœ… Real-life Example 2:

htmlCopyEdit<button id="submitBtn">Submit</button>
javascriptCopyEdit$("#submitBtn").hide();

๐Ÿง  Hides the Submit button when some condition is met.


๐Ÿ”ถ 2. Class Selector (.class)

Used to select all elements with a specific class.

Syntax: $(".className")

โœ… Real-life Example 1:

htmlCopyEdit<p class="info">This is important info.</p>
<p class="info">Read this carefully.</p>
javascriptCopyEdit$(".info").css("font-weight", "bold");

๐Ÿง  Makes all .info paragraphs bold.

โœ… Real-life Example 2:

htmlCopyEdit<button class="btn">Save</button>
<button class="btn">Cancel</button>
javascriptCopyEdit$(".btn").addClass("active");

๐Ÿง  Adds an "active" style to all buttons.


๐ŸŸข 3. Element Selector (div, p, h1, etc.)

Used to select all elements of a specific type.

Syntax: $("tagname")

โœ… Real-life Example 1:

htmlCopyEdit<p>Paragraph one</p>
<p>Paragraph two</p>
javascriptCopyEdit$("p").css("color", "green");

๐Ÿง  Changes the color of all <p> elements to green.

โœ… Real-life Example 2:

htmlCopyEdit<div>Box 1</div>
<div>Box 2</div>
javascriptCopyEdit$("div").slideUp();

๐Ÿง  Slides up (hides with animation) all divs.


๐ŸŸก 4. Attribute Selector ([attribute="value"])

Used to select elements with a specific attribute.

Syntax: $("[attribute='value']")

โœ… Real-life Example 1:

htmlCopyEdit<input type="text" placeholder="Enter name">
<input type="password">
javascriptCopyEdit$("input[type='text']").val("John Doe");

๐Ÿง  Automatically fills in the name field with "John Doe".

โœ… Real-life Example 2:

htmlCopyEdit<input type="checkbox" name="subscribe">
<input type="radio" name="gender">
javascriptCopyEdit$("input[type='checkbox']").prop("checked", true);

๐Ÿง  Checks the checkbox automatically.


๐Ÿงฉ Complete Real-Life Example:

๐ŸŽฏ Scenario:

Letโ€™s say you are building a registration form. When a user clicks the "Register" button:

  • You want to highlight the input fields,

  • Hide the button after clicking,

  • And show a success message.

๐Ÿ’ป HTML:

htmlCopyEdit<h2 id="formTitle">Register Now</h2>
<input type="text" class="inputField" placeholder="Enter your name">
<input type="email" class="inputField" placeholder="Enter your email">
<button id="registerBtn">Register</button>
<p id="message" style="display:none;">๐ŸŽ‰ You are registered!</p>

โœจ jQuery:

javascriptCopyEdit$("#registerBtn").click(function(){
  $(".inputField").css("border", "2px solid green");
  $("#registerBtn").hide();
  $("#message").show();
});

๐Ÿง  What it does:

  • When you click the Register button,

  • It adds a green border to all input fields,

  • Hides the button,

  • And shows a hidden success message.

0
Subscribe to my newsletter

Read articles from Payal Porwal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Payal Porwal
Payal Porwal

Hi there, tech enthusiasts! I'm a passionate Software Developer driven by a love for continuous learning and innovation. I thrive on exploring new tools and technologies, pushing boundaries, and finding creative solutions to complex problems. What You'll Find Here On my Hashnode blog, I share: ๐Ÿš€ In-depth explorations of emerging technologies ๐Ÿ’ก Practical tutorials and how-to guides ๐Ÿ”งInsights on software development best practices ๐Ÿš€Reviews of the latest tools and frameworks ๐Ÿ’ก Personal experiences from real-world projects. Join me as we bridge imagination and implementation in the tech world. Whether you're a seasoned pro or just starting out, there's always something new to discover! Letโ€™s connect and grow together! ๐ŸŒŸ