Web Designing 2 — Study Notes

Unit 02

jQuery — Selectors, Events, Effects & Manipulation

4 Questions 6 Marks Each Web Designing 2
01
6 Marks Question

Advantages of jQuery and Syntax

Introduction

jQuery is a fast, small, and feature-rich JavaScript library that simplifies client-side scripting of HTML. Its motto is "Write Less, Do More."

Advantages of jQuery

Simple and Short Syntax
jQuery code is easy to write and understand. It requires fewer lines of code compared to plain JavaScript.
Cross-Browser Compatibility
The same jQuery code works across major browsers like Chrome, Firefox, Edge, and Safari.
Powerful Selectors
jQuery uses CSS-style selectors to easily select HTML elements. Example: $("p.intro") selects all paragraphs with class intro.
Built-in Effects and Animations
Provides ready-made functions like hide(), show(), fadeIn(), etc., without complex coding.
AJAX Support
Makes asynchronous data loading and server communication simple.
Large Community and Plugins
Thousands of plugins are available for validation, sliders, UI components, and more.
Fast Performance
Lightweight and optimized library for better performance.

jQuery Syntax

The basic jQuery syntax is:

Basic Syntax
$(selector).action();
$ jQuery symbol — tells the browser to use jQuery.
selector Selects HTML elements to operate on.
action() Performs the operation on selected elements.

Example

$(document).ready(function(){
  $("p").hide();
});
Meaning: Waits until the document is fully loaded, then hides all <p> elements. The $(document).ready() method ensures code runs only after the page loads.
Conclusion jQuery simplifies JavaScript programming by providing short syntax, powerful selectors, cross-browser support, and built-in effects.
02
6 Marks Question

jQuery Selectors

Definition

jQuery selectors are used to select and manipulate HTML elements. They work similarly to CSS selectors and allow easy access to elements in the DOM.

The selector syntax always starts with the dollar sign:

$(selector)

Types of jQuery Selectors

Element Element Selector

Selects elements based on the HTML tag name.

Syntax
$("element-name")
Example
$("p")
→ Selects all <p> elements.
#id ID Selector

Selects a single element using its unique id.

Syntax
$("#id")
Example
$("#header")
→ Selects the element with id="header".
.class Class Selector

Selects all elements having a specific class name.

Syntax
$(".class-name")
Example
$(".intro")
→ Selects all elements with class="intro".

Example Program

<p class="intro">Hello</p>
<p id="msg">Welcome</p>

<script>
$(document).ready(function(){
  $(".intro").hide();        // class selector
  $("#msg").show();          // id selector
  $("p").css("color","red"); // element selector
});
</script>
Conclusion jQuery selectors provide a simple and powerful way to find and manipulate HTML elements using CSS-like syntax, making DOM operations faster and easier.
03
6 Marks Question

jQuery Events and Effects

jQuery Events

Definition

jQuery events are actions that occur when a user interacts with a web page, such as clicking, typing, or submitting a form. jQuery makes event handling easy and cross-browser compatible.

Common jQuery Events

click() Triggered when an element is clicked.
dblclick() Triggered on double click.
hover() Triggered when mouse enters and leaves.
focus() When input field gets focus.
blur() When input field loses focus.
change() When value of input changes.
submit() When a form is submitted.
keydown() When a key is pressed.

Example

$("#loginBtn").click(function(){
  alert("Login button clicked");
});

This runs the function when the button with id loginBtn is clicked.

Document Ready Event

$(document).ready(function(){
  // jQuery code here
});

It ensures the code runs only after the page is fully loaded.

jQuery Effects

Definition

jQuery effects are used to add visual animations and dynamic behavior to web pages with minimal code. jQuery effects include showing, hiding, fading, sliding, and custom animations.

Common jQuery Effects

show() Displays hidden elements.
hide() Hides visible elements.
toggle() Switches between show and hide.
fadeIn() Fades element into visibility.
fadeOut() Fades element out of visibility.
fadeTo() Fades to a specific opacity level.
animate() Performs custom CSS animation.

Example

$("#box").fadeIn(1000);

This fades in the element with id box over 1 second (1000 milliseconds).

Conclusion jQuery events handle user interactions, while jQuery effects provide animations and visual enhancements, making web pages more dynamic and interactive.
04
6 Marks Question

jQuery Manipulation Methods

Definition

jQuery manipulation methods are used to modify the content, attributes, CSS, and structure of HTML elements dynamically. They make DOM manipulation simple and efficient.

Types of jQuery Manipulation Methods

Content Content Manipulation Methods

These methods change the text or HTML inside elements.

text() Sets or gets plain text content.
html() Sets or gets HTML content.
val() Sets or gets value of form fields.
Example
$("#msg").text("Hello");

Changes the text content of the element with id msg.

Attribute Attribute Manipulation Methods

Used to get or change element attributes.

attr() Sets or gets attribute values.
removeAttr() Removes an attribute from element.
Example
$("#img").attr("src", "pic.jpg");

Changes the src attribute of the image element.

CSS CSS Manipulation Methods

Used to modify styles and classes of elements dynamically.

css() Gets or sets a CSS property.
addClass() Adds a class to the element.
removeClass() Removes a class from the element.
toggleClass() Toggles a class on or off.
Example
$("p").css("color", "red");

Changes the text color of all <p> elements to red.

DOM DOM Structure Manipulation

Used to add or remove HTML elements from the page.

append() Adds content at the end of element.
prepend() Adds content at the beginning.
remove() Removes the selected element.
empty() Removes all child elements.
Example
$("#list").append("<li>New Item</li>");

Adds a new list item at the end of the element with id list.

Conclusion jQuery manipulation methods allow developers to easily modify HTML content, attributes, styles, and page structure, making web pages dynamic and interactive.