jQuery & DOM Manipulation

What is jQuery & Why Use It?

10 min Lesson 1 of 30

Welcome to jQuery!

jQuery is a fast, small, and feature-rich JavaScript library that makes web development easier and more efficient. Created by John Resig in 2006, it has become one of the most popular JavaScript libraries in the world.

What is jQuery?

jQuery is a JavaScript library that simplifies:

  • DOM Manipulation - Selecting and modifying HTML elements
  • Event Handling - Responding to user interactions
  • Animations - Creating smooth visual effects
  • AJAX - Making asynchronous HTTP requests
  • Cross-browser Compatibility - Writing code that works everywhere

Why Use jQuery?

💡 Key Benefits:
  • Write Less, Do More - jQuery's motto says it all! Complex JavaScript tasks become simple one-liners.
  • Easy to Learn - Intuitive syntax that's beginner-friendly
  • Cross-browser Support - Works consistently across all browsers
  • Large Ecosystem - Thousands of plugins and extensive documentation
  • Active Community - Millions of developers worldwide

jQuery vs Vanilla JavaScript

Let's compare a simple task - hiding an element:

Vanilla JavaScript:
document.getElementById('myElement').style.display = 'none';
jQuery:
$('#myElement').hide();

Notice how jQuery is more concise and readable!

Real-World Use Cases

jQuery is perfect for:

  • Creating interactive forms with validation
  • Building dynamic navigation menus
  • Implementing image sliders and galleries
  • Loading content without page refresh (AJAX)
  • Adding smooth animations and transitions
  • Creating modal dialogs and tooltips

Getting Started with jQuery

To use jQuery in your project, you need to include the library. There are two main ways:

1. Using a CDN (Content Delivery Network):
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
2. Downloading and Hosting Locally:
<script src="js/jquery-3.7.1.min.js"></script>
📝 Note: We recommend using a CDN for faster loading times and automatic caching. Place the jQuery script tag before your custom JavaScript files.

jQuery Versions

jQuery has different versions:

  • 3.x - Latest version with modern features (recommended)
  • 2.x - No support for IE 6-8
  • 1.x - Legacy version supporting older browsers
⚠️ Important: Always use the latest stable version (3.7.1 as of 2024) unless you need to support very old browsers.

Your First jQuery Code

Here's a complete example showing jQuery in action:

<!DOCTYPE html>
<html>
<head>
    <title>My First jQuery Page</title>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
</head>
<body>
    <h1 id="heading">Hello World!</h1>
    <button id="changeBtn">Click Me</button>

    <script>
        $('#changeBtn').click(function() {
            $('#heading').text('Hello jQuery!');
        });
    </script>
</body>
</html>

This code changes the heading text when you click the button. Simple, right?

🎯 Quick Exercise:

Set up a new HTML file and include jQuery using the CDN method. Create a button that, when clicked, shows an alert saying "jQuery is working!". This will confirm your jQuery setup is correct.

💡 Solution Hint
$('#myButton').click(function() {
    alert('jQuery is working!');
});

When NOT to Use jQuery

While jQuery is powerful, modern JavaScript (ES6+) has made some jQuery features less necessary:

  • Simple DOM selection (querySelector is now widely supported)
  • Basic AJAX (Fetch API is now standard)
  • Modern frameworks like React, Vue, or Angular (they have their own DOM handling)
💡 Pro Tip: Learning jQuery is still valuable because:
  • Millions of websites still use it
  • Many legacy projects require jQuery knowledge
  • It teaches important JavaScript concepts
  • It's still the fastest way for rapid prototyping

Summary

In this lesson, you learned:

  • jQuery is a JavaScript library that simplifies web development
  • It makes DOM manipulation, events, and AJAX easier
  • jQuery code is more concise than vanilla JavaScript
  • You can include jQuery via CDN or local download
  • jQuery is perfect for rapid development and cross-browser compatibility

In the next lesson, we'll dive into jQuery syntax and the famous "document ready" function!

ES
Edrees Salih
6 hours ago

We are still cooking the magic in the way!