Unit 6 Jquery
Unit 6 Jquery
JQUERY
JQUERY
• jQuery is a popular and easy-to-use JavaScript library.
• It's like a toolbox for web developers that makes it simpler to do things on
websites.
• It helps them add interactive features, animations, and manipulate web page
elements without writing as much complex code as they would have to with
just plain JavaScript.
• In simple terms, you can think of jQuery as a set of pre-made tools that web
developers can use to make their websites more dynamic and user-friendly.
The jQuery library contains the following features:
•HTML/DOM manipulation
•CSS manipulation
•HTML event methods
•Effects and animations
•AJAX
•Utilities
(notice that the <script> tag should be inside the <head> section)
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script src="js/jquery.js"></script>
</head>
<body>
<script>
$(document).ready(function(){
alert(1);
});
</script>
</body>
</html>
Adding jQuery to Your Web Pages
If you don't want to download and host jQuery yourself, you can include it from a CDN
(Content Delivery Network).
<!DOCTYPE html>
<html lang="en">
<head>
<title>Document</title>
<script
src="https://code.jquery.com/jquery-3.7.1.min.js"
integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo="
crossorigin="anonymous"></script>
</head>
<body>
<script>
$(document).ready(function(){
alert(1);
});
</script>
</body>
</html>
BASIC SYNTAX
BASIC SYNTAX
jQuery Syntax
Basic syntax is: $(selector).action()
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
elements.
• A jQuery Selector is a function which makes use of
expressions to find out matching elements from a DOM
based on the given criteria.
• jQuery selectors are used to "find" (or select) HTML
elements based on their id, classes, types, attributes, values
of attributes and much more.
• All selectors in jQuery start with the dollar sign and
parentheses: $().
• Selector with tag name
• Represents a tag name available in the DOM.
• For example $('p') selects all paragraphs in the document.
• Selector with ID
• Represents a tag available with the given ID in the DOM.
• For example$('#some-id') selects the single element in the
document that has an ID of some-id.
• Selector with class
• Represents a tag available with the given class in the DOM.
• For example$('.some-class') selects all elements in the document
that have a class of some-class.
• $('*')
• This selector selects all elements in the document.
• $("ul li:first")
• This selector gets only the first <li> element of the <ul>.
• $(“tr:even")
• Selects all even indexed <tr> in table.
• $(":radio")
• Selects all radio buttons in the form.
• $(":text")
• Selects only text elements (input[type=text]).
jQuery Event Methods
• All the different visitors' actions that a web page can respond to are
called events.
• An event represents the precise moment when something happens.
Examples:
•moving a mouse over an element
•selecting a radio button
•clicking on an element
jQuery Event Methods
Commonly Used jQuery Event Methods
$(document).ready()
The $(document).ready() method allows us to execute a function when the document is fully
loaded.
click()
The click() method attaches an event handler function to an HTML element.
The function is executed when the user clicks on the HTML element.
The following example says: When a click event fires on a <p> element; hide the current <p> element:
<html>
<head>
<title>The jQuery Example</title>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type = "text/javascript">
$(document).ready(function()
{
$("#id1").click(function()
{
alert("Welcome to Jquery");
});
});
</script>
</head>
<body>
<button id="id1">Click here for message</button>
<p id="id1">Click here for message</p>
</body>
</html>
<html>
<head>
<script src="jquery-1.8.2.min.js"></script>
<script>
$(document).ready(function(){
$("#click1").dblclick(function(){
$(this).hide();
});
$(".click2").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you double-click on me, I will disappear.</p>
<p id="click1">Double click here!</p>
<p class="click2">Single click here!</p>
</body>
</html>
jQuery Effects - Hide and
Show
<html>
<head>
<title>The jQuery Example</title>
<script type="text/javascript" src="jquery-1.8.2.min.js"></script>
<script type = "text/javascript">
$(document).ready(function(){
$("#b1").click(function(){
$("p").show();
});
$("#b2").click(function(){
$("p").hide();
});
});
</script>
</head>
<body>
<p>Hide and Seek</p>
<input type="button" id="b1" value="show paragraph">
<input type="button" id="b2" value="hide paragrphh">
</body>
</html>
jQuery toggle()
You can also toggle between hiding and showing an element with
the toggle() method.
Shown elements are hidden and hidden elements are shown:
<script src="jquery-1.8.2.min.js"></script>
<script>
$(document).ready(function()
{
$("button").click(function()
{
$("p").toggle(500);
});
});
</script>
</head>
<body>
<button>Toggle</button>
<p>This is a paragraph with little content.</p>
<p>This is another small paragraph.</p>
jQuery Animation Effects
<html>
<head>
<title>The jQuery Example</title>
<script type = "text/javascript" src ="jquery-1.8.2.min.js"></script>
<script type = "text/javascript" language = "javascript">
$(document).ready(function() {
$("#in").click(function(){
$("#img1").fadeIn( 'slow', function(){
});
});
$("#out").click(function(){
$("#img1").fadeOut( 'slow', function(){
});});});
</script></head><body>
<p>Click on any of the buttons</p>
<button id = "out"> Fade Out </button>
<button id = "in"> Fade In</button>
<br><br>
<img src = "smiley1.jpg" id="img1" alt = "jQuery"></body></html>
JavaScript HTML DOM
• When a web page is loaded, the browser creates a Document Object Model of the page.
</script>
</body>
</html>
<html>
<head>
<title></title>
</head>
<body>
<h1> location object methods</h1>
<button onclick="newFunction()">Click</button>
<script>
function newFunction()
{
location.assign("https://www.google.com");
}
</script>
</body>
</html>