๐ง Introduction to jQuery & jQuery Selectors ๐


๐ง 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.
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! ๐