Mastering jQuery: From DOM Manipulation to Dynamic Interactivity

4.73K 0 0 0 0

Chapter 2: Introduction to jQuery and DOM Manipulation

🔹 What is jQuery and Why It Matters

jQuery is a lightweight, "write less, do more" JavaScript library that simplifies programming with JavaScript and improves the way developers write code for DOM manipulation, animations, event handling, and AJAX operations.

Key Benefits:

Feature

Description

Cross-Browser Support

Fixes JavaScript inconsistencies across browsers

Simplicity

Easy syntax for complex tasks

Less Code

Achieve more with fewer lines of code

Plugins

Huge plugin ecosystem for UI components

AJAX

Easy asynchronous request handling

“jQuery made JavaScript approachable — and it's still useful today in many legacy systems and WordPress environments.”


🔹 How to Include jQuery in Your Project

You can include jQuery via CDN (recommended) or host it locally.

Method 1: Using CDN

<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

Method 2: Local Installation

<script src="js/jquery.min.js"></script>

Make sure the <script> tag is placed before the closing </body> tag so it loads after the HTML.


🔹 Syntax Basics: Selectors, Events, and Chaining

🔸 Basic Syntax

$(selector).action()

  • $: jQuery identifier
  • selector: selects HTML elements (like "#id" or ".class")
  • action(): function to perform on the selected elements

Examples:

$("#heading").hide();            // Hides an element with ID 'heading'

$(".btn").css("color", "red");   // Changes color of all elements with class 'btn'

🔸 Event Handling

$(selector).on(event, function)

Example:

$("#btn").on("click", function() {

  alert("Button clicked!");

});

🔸 Chaining

$("#box").css("color", "blue").slideUp(2000).slideDown(2000);

Chaining allows multiple actions on the same element with less code.


🔹 DOM Traversal and Manipulation

jQuery provides powerful tools to manipulate and traverse the DOM easily.


🔸 .html(), .text(), .val()

Method

Purpose

Example

.html()

Gets or sets HTML content

$("#demo").html("<b>Hello</b>")

.text()

Gets or sets text content

$("#demo").text("Hello")

.val()

Gets or sets input values

$("#input").val("New Value")


🔸 .attr() – Get or Set Attributes

// Get attribute

var href = $("a").attr("href");

 

// Set attribute

$("a").attr("target", "_blank");


🔸 Add / Remove Elements

$("#list").append("<li>New item</li>");  // Adds at end

$("#list").prepend("<li>First item</li>");  // Adds at start

$("#list li:last").remove();             // Removes last item


🔸 Table Example (DOM Manipulation)

<table id="userTable">

  <tr><td>Name</td><td>Age</td></tr>

</table>

<button id="addRow">Add Row</button>

javascript

CopyEdit

$("#addRow").click(function(){

  $("#userTable").append("<tr><td>John</td><td>30</td></tr>");

});


🔹 Summary Table: Common jQuery Methods

Task

jQuery Method

Example

Get/Set HTML

.html()

$("#demo").html("Hello")

Get/Set Text

.text()

$(".box").text("Hi")

Get/Set Input Value

.val()

$("#name").val()

Get/Set Attribute

.attr()

$("a").attr("href")

Hide/Show Element

.hide() / .show()

$("#item").hide()

Event Binding

.on()

$("#btn").on("click", fn)

Chaining

Multiple methods

$("#el").hide().fadeIn()



Back

FAQs


1. What is jQuery?

jQuery is a fast, small JavaScript library that simplifies HTML document traversal, manipulation, event handling, and AJAX.

2. Is jQuery still relevant today?

Yes while modern frameworks exist, jQuery remains heavily used in WordPress, older projects, CMSs, and quick prototypes.

3. What are the benefits of using jQuery?

It shortens code, handles cross-browser issues, simplifies AJAX, and is beginner-friendly.

4. How do I include jQuery in a webpage?

  1. Use a CDN:


<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

5. What is the difference between $(document).ready() and window.onload?

$(document).ready() runs when the DOM is ready; window.onload waits for full page (including images) to load.

6. Can I use jQuery with other JavaScript libraries?

$(document).ready() runs when the DOM is ready; window.onload waits for full page (including images) to load

7. Is jQuery free to use in commercial projects?

Yes its open-source under the MIT license.

8. What's the difference between jQuery and JavaScript?

jQuery is a library built with JavaScript to simplify tasks like DOM handling and AJAX.

9. Does jQuery work on mobile browsers?

Yes it works across all major desktop and mobile browsers.

10. Should I learn jQuery before JavaScript?

It’s better to learn JavaScript fundamentals first, but jQuery is easier to grasp early on for quick UI work.