Module 4
Module 4
Module 4
jQuery
What is jQuery
• jQuery is a small and lightweight JavaScript library.
• jQuery is cross-platform.
• jQuery means "write less do more".
• jQuery simplifies AJAX call and DOM manipulation.
• It is designed to simplify the client-side scripting of HTML.
• It makes things like HTML document traversal and manipulation, animation, event handling,
and
AJAX very simple with an easy-to-use API that works on a lot of different type of browsers.
• The main purpose of jQuery is to provide an easy way to use JavaScript on your website to
make it
more interactive and attractive. It is also used to add animation.
jQuery Features
• HTML manipulation
• DOM manipulation
• DOM element selection
• CSS manipulation
• Effects and Animations
• Utilities
• AJAX
• HTML event methods
• JSON Parsing
• Extensibility through plug-ins
Advantages:
• Wide range of plug-ins that allows developers to create plug-ins on top of the JavaScript
library.
• Large development community.
• It is a lot easier to use compared to standard JavaScript and other JavaScript libraries.
• It lets users develop Ajax templates with ease.
• Being Light weight and a powerful chaining capabilities makes it more strong.
jQuery Syntax
Basic syntax is:
$(selector).action()
A $ sign to define/access jQuery
A (selector) to "query (or find)" HTML elements
A jQuery action() to be performed on the element(s)
Examples:
$(this).hide() - hides the current element.
$("p").hide() - hides all <p> elements.
$(".test").hide() - hides all elements with class="test".
$("#test").hide() - hides the element with id="test"
jQuery Selectors
• jQuery selectors allow you to select and manipulate HTML element(s).
• jQuery selectors are used to "find" (or select) HTML elements based on their name, id,
classes,
• types, attributes, values of attributes and much more. It's based on the existing CSS Selectors,
and
• in addition, it has some own custom selectors.
• All selectors in jQuery start with the dollar sign and parentheses: $(). It is known as the
factory
• function.
Parameters:
*: This parameter is used to select all elements.
Example:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("*").hide();
});
});
</script>
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Click me to hide paragraph </button>
</body>
</html>
jQuery #id Selector
• The #id selector specifies an id for an element to be selected.
• It should not begin with a number and the id attribute must be unique within a document
which
• means it can be used only one time.
Syntax:
$("#id")
Parameter:
id: An element’s specific id.
Example:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#test").hide();
});
});
</script>
</head>
<body>
<p>hi</p>
<p id="test">welcome</p>
<button>Click me</button>
</body>
</html>
Example:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script>
<script>
$(document).ready(function () {
$("p:last").css("background-color", "yellow");
});
</script>
</head>
<body>
<p>welcome</p>
<p>jQuery</p>
<p>First Selector</p>
</body>
</html>
Syntax:
$(":odd")
Example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
$("tr:odd").css("background-color","blue");
});
</script>
</head>
<body>
<table border="1">
<tr> <th>Company</th> <th>Country</th> </tr>
<tr> <td>reliance</td> <td>India</td> </tr>
</table>
</body>
</html>
jQuery Events
• jQuery events are the actions that can be detected by your web application. They are used to
create dynamic web pages.
• An event shows the exact moment when something happens.
• examples of events.
• A mouse click
• An HTML form submission
• A web page loading
• A keystroke on the keyboard
• Scrolling of the web page etc.
jQuery click()
• When you click on an element, the click event occurs and once the click event occurs it
execute
• the click () method or attaches a function to run.
• It is generally used together with other events of jQuery.
Syntax:
$(selector).click()
• It is used to trigger the click event for the selected elements.
$(selector).click(function)
• It is used to attach the function to the click event.
Example
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
alert("This paragraph was clicked.");
});
});
</script>
</head>
<body>
<p>Click on the statement.</p>
</body>
</html>
jQuery bind()
• The jQuery bind() event is used to attach one or more event handlers for selected
• elements from a set of elements.
• It specifies a function to run when the event occurs.
• It is generally used together with other events of jQuery.
Syntax:
$(selector).bind(event,data,function,map)
Parameters of jQuery bind() event
Parameter Description
Event: It is a mandatory parameter. It specifies one or more events to attach to the elements.
Data: It is an optional parameter. It specifies additional data to pass along to the function.
Function: It is a mandatory parameter. It executes the function to run when the event occurs.
Map: It specifies an event map which contains one or more events or functions attached to
the element.
jQuery keydown()
• When you press a key on the keyboard, the keydown() event is occurred and once the keydown()
event is occurred, it executes the function associated with keydown() method to run.
The keydown() event is generally used with two other events.
Example
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
$("input").keydown(function(){
$("input").css("background-color", "green");
});
$("input").keyup(function(){
$("input").css("background-color", "violet");
});
});
</script>
</head>
<body>
Write something: <input type="text">
</body>
</html>
jQuery hover()
• The jQuery hover() method executes two functions when you roam the mouse pointer over
the selected element.
• The hover() method triggers both the mouseenter and mouseleave events.
Syntax:
$(selector).hover(inFunction,outFunction)
Parameters of jQuery hover() event
Parameter Description
InFunction: It is a mandatory parameter. It is executed the function when mouseenter event
occurs.
OutFunction: It is an optional parameter. It is executed the function when mouseleave event occurs.
Example
<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function(){
$("p").hover(function(){
$(this).css("background-color", "violet");
}, function(){
$(this).css("background-color", "green");
});
});
</script>
</head>
<body>
<p>Hover your mouse pointer on me!</p>
</body>
</html>
jQuery submit()
• jQuery submit event is sent to the element when the user attempts to submit a form.
• This event is only attached to the <form> element. Forms can be submitted either by clicking on
the submit button or by pressing the enter button on the keyboard when that certain form elements
have focus. When the submit event occurs, the submit() method attaches a function with it to run.
Syntax:
$(selector).submit()
It triggers the submit event for selected elements.
$(selector).submit(function)
It adds a function to the submit event.
Parameters of jQuery submit() event
Parameter Description
Function: It is an optional parameter. It is used to specify the function to run when the submit
event is executed.
jQuery focus()
The jQuery focus event occurs when an element gains focus. It is generated by a mouse click or by
navigating to it.
This event is implicitly used to limited sets of elements such as form elements like <input>, <select>
etc. and links <a href>. The focused elements are usually highlighted in some way by the browsers.
The focus method is often used together with blur () method.
Syntax:
$(selector).focus()
It triggers the focus event for selected elements.
$(selector).focus(function)
It adds a function to the focus event.
Parameters of jQuery focus() event
Parameter Description
Function: It is an optional parameter. It is used to specify the function to run when the element gets
the focus.
jQuery blur()
• The jQuery blur event occurs when element loses focus. It can be generated by via keyboard
commands like tab key or mouse click anywhere on the page.
• It makes you enable to attach a function to the event that will be executed when the element loses
focus. Originally, this event was used only with form elements like <input>. In latest browsers, it has
been extended to include all element types.
• The blur () method is often used together with focus () method.
Syntax:
$(selector).blur()
jQuery Effects
jQuery enables us to add effects on a web page. jQuery effects can be categorized into fading, sliding,
hiding/showing and animation effects.
jQuery hide()
The jQuery hide() method is used to hide the selected elements.
Syntax:
$(selector).hide();
$(selector).hide(speed, callback);
$(selector).hide(speed, easing, callback);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible vales are slow, fast
and milliseconds.
easing: It specifies the easing function to be used for transition.
callback: It is also an optional parameter. It specifies the function to be called after completion of
hide() effect.
jQuery show()
The jQuery show() method is used to show the selected elements.
Syntax:
$(selector).show();
$(selector).show(speed, callback);
$(selector).show(speed, easing, callback);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible vales are slow, fast
and milliseconds.
easing: It specifies the easing function to be used for transition.
callback: It is also an optional parameter. It specifies the function to be called after completion of
show() effect.
jQuery fadeIn()
jQuery fadeIn() method is used to fade in the element.
Syntax:
$(selector).fadein();
$(selector).fadeIn(speed,callback);
$(selector).fadeIn(speed, easing, callback);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible vales are slow, fast
and milliseconds.
easing: It specifies the easing function to be used for transition.
callback: It is also an optional parameter. It specifies the function to be called after completion of
fadein() effect.
jQuery fadeOut()
The jQuery fadeOut() method is used to fade out the element.
Syntax:
$(selector).fadeOut();
$(selector).fadeOut(speed,callback);
$(selector).fadeOut(speed, easing, callback);
speed: It is an optional parameter. It specifies the speed of the delay. Its possible vales are slow, fast
and milliseconds.
easing: It specifies the easing function to be used for transition.
callback: It is also an optional parameter. It specifies the function to be called after completion of
fadeOut() effect.
jQuery slideUp()
jQuery slideDown() method is used to slide up an element.
Syntax:
$(selector).slideUp(speed);
$(selector).slideUp(speed, callback);
$(selector).slideUp(speed, easing, callback);
speed: It specifies the speed of the delay. Its possible vales are slow, fast and milliseconds.
easing: It specifies the easing function to be used for transition.
callback: It is also an optional parameter. It specifies the function to be called after completion of
slideUp() effect.
jQuery slideDown()
jQuery slideDown() method is used to slide down an element.
Syntax:
$(selector).slideDown(speed);
$(selector).slideDown(speed, callback);
$(selector).slideDown(speed, easing, callback);
speed: It specifies the speed of the delay. Its possible vales are slow, fast and milliseconds.
easing: It specifies the easing function to be used for transition.
callback: It is also an optional parameter. It specifies the function to be called after completion of
slideDown() effect.
jQuery animate()
The jQuery animate() method provides you a way to create custom animations.
Syntax:
$(selector).animate({params}, speed, callback);
Here, params parameter defines the CSS properties to be animated.
The speed parameter is optional and specifies the duration of the effect. It can be set as "slow" , "fast"
or milliseconds.
The callback parameter is also optional and it is a function which is executed after the animation
completes.
jQuery html()
• jQuery html() method is used to change the entire content of the selected elements.
• It replaces the selected element content with new contents.
• The first method signature has no argument, so it just returns the HTML within that
element.
• The remaining two signatures take a single argument: i.e. a string or a function that
returns a string.
Syntax:
$(selector).html()
It is used to return content.
$(selector).html(content)
It is used to set content.
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").html("Hello <b>welcome</b>");
});
});
</script>
</head>
<body>
jQuery css()
The jQuery CSS() method is used to get (return)or set style properties or values for selected elements.
It facilitates you to get one or more style properties.
jQuery CSS() method provides two ways:
jQuery before()
The jQuery before() method is used to insert the specified content before the selected elements.
It adds the content specified by the parameter, before each element in the set of matched elements.
Syntax:
$(selector).before(content, function(index))
jQuery append()
The jQuery append() method is used to insert specified content as the last child (at the end of) the
selected elements in the jQuery collection.
The append () and appendTo () methods are used to perform the same task. The only difference
between them is in the syntax.
Syntax:
$(selector).append(content, function(index, html))
jQuery prepend()
The jQuery prepend() method is used to insert the specified content at the beginning (as a first child)
of the selected elements.
It is just the opposite of the jQuery append() method.
If you want to insert the content at the end of the selected elements, you should use the append
method.
Syntax:
$(selector).prepend(content,function(index,html))
jQuery width()
jQuery width() method is used to return or set the width of matched element.
To return width: When this method is used to return the width, it returns the width of first matched
element.
To set width:When this method is used to set the width, it sets the width for every matched element.
Syntax:
To return the width:
$(selector).width()
To set the width:
$(selector).width(value)
To set the width using a function:
$(selector).width(function(index,currentwidth))
jQuery height()
The jQuery height() method is used for two purposes:
To return height: When this method is used to return height, it returns the height of first matched
element.
To set height: When this method is used to set height, it sets height of all matched elements.
This method is a very common jQuery dimension.
Syntax:
To return the height:
$(selector).height()
jQuery wrapAll()
jQuery wrapAll() method is used to wrap specified HTML elements around all selected elements, in
a set of matched elements.
Syntax:
$(selector).wrapAll(wrappingElement)
jQuery unwrap()
The jQuery unwrap() method is used to remove the parent element of the selected elements.
Syntax:
$(selector).unwrap()