J Query 2
J Query 2
Example
<script>
$(document).ready(function(){
// Hide displayed paragraphs
$(".hide-btn").click(function(){
$("p").hide();
});
Example:
<!DOCTYPE html>
<html>
<head>
<script src="jquery-3.6.4.min.js"></script>
<script>
$(document).ready(function(){
// Hide displayed paragraphs
$(".hide-btn").click(function(){
$("p").hide();
});
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button class = "hide-btn" >Click me to hide paragraphs</button>
<button class = "show-btn">Click me to show paragraphs</button>
</body>
</html>
Example
<script>
$(document).ready(function(){
// Toggles paragraphs display
$(".toggle-btn").click(function(){
$("p").toggle();
});
});
</script>
Similarly, It can specify the duration parameter for the toggle() method to make it animated
like the show() and hide() methods.
Example
<script>
$(document).ready(function(){
// Fading out displayed paragraphs
$(".out-btn").click(function(){
$("p").fadeOut();
});
<script>
$(document).ready(function(){
// Fading out displayed paragraphs
$(".out-btn").click(function(){
$("p").fadeOut();
});
</head>
<body>
<h2>This is a heading</h2>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button class = "out-btn" >Click me to fade out </button>
<button class = "in-btn" >Click me to fade in </button>
</body>
</html>
Example
<script>
$(document).ready(function(){
// Slide up displayed paragraphs
$(".up-btn").click(function(){
$("p").slideUp();
});
Syntax
The basic syntax of the jQuery animate() method can be given with:
$(selector).animate({ properties }, duration, callback);
Here's a simple example of the jQuery animate() method that animates an image from its
original position to the right by 300 pixels on click of the button.
Example
<script>
$(document).ready(function(){
$("button").click(function(){
$("img").animate({
left: 300
});
});
});
</script>
Events in jQuery
Events are often triggered by the user's interaction with the web page, such as when a link or button is clicked,
text is entered into an input box or textarea, selection is made in a select box, key is pressed on the keyboard,
the mouse pointer is moved etc. In some cases, the Browser itself can trigger the events, such as the page load
and unload events.
jQuery enhances the basic event-handling mechanisms by offering the events methods for most native browser
events, some of these methods are ready(), click(), keypress(), focus(), blur(), change(), etc.
For example, to execute some JavaScript code when the DOM is ready, It can use the
jQuery ready() method, like this:
Example
<script>
$(document).ready(function(){
// Code to be executed
alert("Hello World!");
});
</script>
Mouse Events
A mouse event is fired when the user click some element, move the mouse pointer etc. Here're some
commonly used jQuery methods to handle the mouse events.
Example
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).slideUp();
});
});
</script>
Example
<script>
$(document).ready(function(){
$("p").dblclick(function(){
$(this).slideUp();
});
});
</script>
Example
<script>
$(document).ready(function(){
$("p").hover(function(){
$(this).addClass("highlight");
}, function(){
$(this).removeClass("highlight");
});
});
</script>
Example
<script>
$(document).ready(function(){
$("p").mouseenter(function(){
$(this).addClass("highlight");
});
});
</script>
Example
<script>
$(document).ready(function(){
$("p").mouseleave(function(){
$(this).removeClass("highlight");
});
});
</script>
Keyboard Events
A keyboard event is fired when the user press or release a key on the keyboard. Here're some commonly used
jQuery methods to handle the keyboard events.
Example
<script>
$(document).ready(function(){
var i = 0;
$('input[type="text"]').keypress(function(){
$("span").text(i += 1);
$("p").show().fadeOut();
});
});
</script>
Example
<script>
$(document).ready(function(){
var i = 0;
$('input[type="text"]').keydown(function(){
$("span").text(i += 1);
$("p").show().fadeOut();
});
});
</script>
Form Events
A form event is fired when a form control receive or loses focus or when the user modify a form control value
such as by typing text in a text input, select any option in a select box etc. Here're some commonly used
jQuery methods to handle the form events.
Example
Try this code »
<script>
$(document).ready(function(){
$("select").change(function(){
var selectedOption = $(this).find(":selected").val();
alert("It have selected - " + selectedOption);
});
});
</script>
Note: For select boxes, checkboxes, and radio buttons, the event is fired immediately when the user makes a
selection with the mouse, but for the text input and textarea the event is fired after the element loses focus.
Example
<script>
$(document).ready(function(){
$("input").focus(function(){
$(this).next("span").show().fadeOut("slow");
});
});
</script>
Example
<script>
$(document).ready(function(){
$("input").blur(function(){
$(this).next("span").show().fadeOut("slow");
});
});
</script>
Example
<script>
$(document).ready(function(){
$("form").submit(function(event){
var regex = /^[a-zA-Z]+$/;
var currentValue = $("#firstName").val();
if(regex.test(currentValue) == false){
$("#result").html('<p class="error">Not valid!</p>').show().fadeOut(1000);
// Preventing form submission
event.preventDefault();
}
});
});
</script>