BWT-Unit IV
BWT-Unit IV
Utility Methods
jQuery offers several utility methods in the $ namespace. These methods are
helpful for accomplishing routine programming tasks. For a complete reference
on jQuery utility methods.
Below are examples of a few of the utility methods:
$.trim()
Removes leading and trailing whitespace:
1 var myArray = [ 1, 2, 3, 5 ];
2
3 if ( $.inArray( 4, myArray ) !== -1 ) {
4 console.log( "found it!" );
5 }
$.extend()
Changes the properties of the first object using the properties of subsequent
objects:
1 var myObject = {
2 myFn: function() {
3 console.log( this );
4 }
5 };
6
7 $( "#foo" ).click( myObject.myFn ); // HTMLElement #foo
8 $( "#foo" ).click( $.proxy( myObject, "myFn" ) ); // myObject
Testing Type
Sometimes the typeof operator can be confusing or inconsistent, so instead of
using typeof, jQuery offers utility methods to help determine the type of a
value.
First of all, you have methods to test if a specific value is of a specific type.
1 $.isArray([]); // true
2 $.isFunction(function() {}); // true
3 $.isNumeric(3.14); // true
Additionally, there is $.type() which checks for the internal class used to create
a value. You can see the method as a better alternative for the typeof operator.
jQuery Events
In most web applications, the user does some action to perform an operation.
For example, user clicks on save button to save the edited data in a web page.
Here, clicking on the button is a user's action, which triggers click event and
click event handler (function) saves data.
Event
The jQuery library provides methods to handle DOM events. Most jQuery
methods correspond to native DOM events.
The following table lists all jQuery methods and corresponding DOM events.
Event Handling
To handle DOM events using jQuery methods, first get the reference of DOM
element(s) using jQuery selector and invoke appropriate jQuery event method.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/aj
ax/libs/jquery/1.11.2/jquery.min.js"
>
</script>
<script>
$(document).ready(function ()
{
$('#saveBtn').click(function
() {
alert('Save
button clicked');
});
});
</script>
</head>
<body>
<h1>Demo: jQuery click()
method</h1>
<h2>Demo: jQuery click()
method</h2>
<input type="button"
value="Save" id="saveBtn" />
</body>
</html>
In the above example, we first use id selector to get a reference of 'Save'
button and then call click method. We have specified handler function as a
callback function, which will be called whenever click event of Save button
is triggered.
Event Object
jQuery passes an event object to every event handler function. The event object
includes important properties and methods for cross-browser consistency e.g.
target, pageX, pageY, relatedTarget etc.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2
/jquery.min.js">
</script>
<script>
$(document).ready(function () {
$('#saveBtn').click(function (eventObj) {
alert('X =' + eventObj.pageX + ', Y =' +
eventObj.pageY);
});
});
</script>
</head>
<body>
<h1>Demo: jQuery eventObject </h1>
$(':button').click(function
(eventObj) {
alert(this.value + ' ' + this.type
+ ' clicked');
});
});
</script>
</head>
<body>
<h1>Demo: jQuery click() method </h1>
You can create separate functions and specify that as a handler. This is useful
if you want to use the same handler for different events or events on different
elements.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/j
query/1.11.2/jquery.min.js">
</script>
<script>
$(document).ready(function () {
var mouseHandler =
function() {
alert('The mouse
entered' );
};
$('#myDiv').on('mouseenter',
mouseHandler);
});
</script>
</head>
<body>
<h1> Demo: Named function as
event handler </h1>
<div id="myDiv"
style="width:100px;height:100px;backgrou
nd-color:red;">
</div>
</body>
</html>
With jQuery, you can hide and show HTML elements with
the hide() and show() methods:
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquer
y.min.js"></script>
<script>
$(document).ready(function(){
$("#hide").click(function(){
$("p").hide();
});
$("#show").click(function(){
$("p").show();
});
});
</script>
</head>
<body>
<button id="hide">Hide</button>
<button id="show">Show</button>
</body>
</html>
Syntax:
$(selector).hide(speed,callback);
$(selector).show(speed,callback);
The optional speed parameter specifies the speed of the hiding/showing, and can
take the following values: "slow", "fast", or milliseconds.
jQuery toggle()
You can also toggle between hiding and showing an element with
the toggle() method.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/j
query.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").toggle();
});
});
</script>
</head>
<body>
</body>
</html>
jQuery Effects - Fading
• fadeIn()
• fadeOut()
• fadeToggle()
• fadeTo()
Syntax:
$(selector).fadeIn(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/j
query.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("#div1").fadeIn();
$("#div2").fadeIn("slow");
$("#div3").fadeIn(3000);
});
});
</script>
</head>
<body>
<div id="div1"
style="width:80px;height:80px;display:none;background
-color:red;"></div><br>
<div id="div2"
style="width:80px;height:80px;display:none;background
-color:green;"></div><br>
<div id="div3"
style="width:80px;height:80px;display:none;background
-color:blue;"></div>
</body>
</html>
• slideDown()
• slideUp()
• slideToggle()
Syntax:
$(selector).slideDown(speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0
/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#flip").click(function(){
$("#panel").slideDown("slow");
});
});
</script>
<style>
#panel, #flip {
padding: 5px;
text-align: center;
background-color: #e5eecc;
border: solid 1px #c3c3c3;
}
#panel {
padding: 50px;
display: none;
}
</style>
</head>
<body>
</body>
</html>
Syntax:
$(selector).animate({params},speed,callback);
The optional speed parameter specifies the duration of the effect. It can take the
following values: "slow", "fast", or milliseconds.
<!DOCTYPE html>
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.6
.0/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("div").animate({left: '250px'});
});
});
</script>
</head>
<body>
<button>Start Animation</button>
<div
style="background:#98bf21;height:100px;width:100
px;position:absolute;"></div>
</body>
</html>
Plug-ins:
A plug-in is piece of code written in a standard JavaScript file. These files provide
useful jQuery methods which can be used along with jQuery library methods.
There are plenty of jQuery plug-in available which you can download from
repository link at https://jquery.com/plugins.
To make a plug-in's methods available to us, we include plug-in file very similar
to jQuery library file in the <head> of the document.
We must ensure that it appears after the main jQuery source file, and before our
custom JavaScript code.
Following example shows how to include jquery.plug-in.js plugin –
How to create a jQuery plugin with methods: In Jquery Plug-in is a code that
is needed in a standard javascript file. Plugins help in providing different
methods that can be used with different jquery library methods.
Created method:
jQuery.fn.methodName = methodDefinition;
• To Obtain a perfect or compatible code use this .each which is used to
perform over a set of matched elements.
• Prefix should conclude with .js.