What Are jQuery Events?
jQuery is the most used JavaScript library for designing web pages. It provides different functions, events, effects, and animation for effective web designing.
jQuery events are various visitor’s actions that a web page can respond to. It is a function that triggers user action. jQuery event has various methods like click, keypress, mouse events, and much more.
All these events contain some action e.g clicking on an element or moving a mouse over an element, etc.
What Does jQuery Events Do?
jQuery events have various types of methods like Mouse Events which include click, dblclick (double click), mouseenter, and mouseleave. It also has different Keyboard Events, Form Events, Custom Event, and Windows Event. Generic Cialis http://www.wolfesimonmedicalassociates.com/cialis/
These events respond to different user actions on web pages. Thus it helps you in creating a responsive web design for your website. Most DOM events in jQuery have equal jQuery methods.
How To Use The jQuery Events?
jQuery Events are easy to define and execute in your HTML code. You just have to follow two steps:
To assign an event to elements on a page, follow syntax:
$("element").event_name();
E.g : $("button").click();
This will assign a click event on all <button> elements.
The next step is to define what action should perform when the event occurs. We need to pass a function to the event.
Syntax:
$("element").event_name(function(){// action});
E.g: $("button").click(function(){// Submit});
jQuery Events are defined within the <script> tag of your HTML file.
Let us see an example using the jQuery “keypress” event to count the number of times the key is pressed on the keyboard. Phentermine 37.5 online http://kendallpharmacy.com/phentermine.html
Code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
i = 0;
$(document).ready(function(){
$("input").keypress(function(){
$("span").text(i += 1);
});
});
</script>
</head>
<body>
<h1>Example to use jQuery "keypress" event</h1>
<h2 style="color:blue">
Enter Text: <input type="text" style="font-size:24px">
<p>Keypress Count: <span>0</span></p>
</h2>
</body>
</html>
Here's how output will be:
What Are The Various jQuery Event Methods?
jQuery provides a variety of event methods to perform various actions and design responsive web pages. These event methods include various Keypress Events, Mouse Events, Form Events, and Windows Events. Some of them are:
1.The dblclick() Event:
This is a mouse event that triggers when an element is double-clicked.
Syntax:
$(selector).dblclick(function)
Here the “function” specifies the action to be performed when double-click event occurs.
E.g :
$("button").dblclick(function(){
alert("The button was double-clicked");
});
2.The resize() Event:
It is a windows event that is triggered when the browser window changes its size. You can count the number of times the size is changed.
Syntax:
$(selector).resize(function)
E.g:
$(window).resize(function(){
$('span').text(x += 1);
});
3.The submit() Event:
This event triggers when a form is submitted. It is used with <form> elements to perform submit action.
Syntax:
$(selector).submit(function)
E.g:
$("form").submit(function(){
alert("Form Submitted Successfully!!");
});
4.The mouseenter() Event:
It is a mouse event that triggers when the mouse pointer enters the selected elements.
Syntax:
$(selector).mouseenter(function)
E.g:
$("button").mouseenter(function(){
$("button").css("background-color", "blue");
});
5.The keyup() Event:
It is a keypress event that is triggered when a keyboard key is released.
Syntax:
$(selector).keyup(function)
E.g:
$("input").keyup(function(){
$("input").css("background-color", "red");
});
Let us see an example to implement the “click” event on all <p> elements.
Code:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<h1>Example to use jQuery "click" Event</h1>
<h2 style="color:red">
<p>I will not be seen if you click on me!</p>
</h2>
</body>
</html>
Here's how output will be:
Before clicking the link
After clicking the link
Conclusion
In this tutorial, we learned what is jQuery events and various event methods. We also learned how to use various jQuery events.
In the above examples, we implemented the jQuery keypress event to count the number of times the key is pressed and the jQuery click method to know on click event.
You can also use other jQuery events for performing various activities in designing your web pages. To learn more jQuery events, subscribe to our blog!
To know more about jQuery AJAX, visit our previous article!