The one() Event is quite similar to the on() event. It is used to attach event/events to an element.
Let us see them one by one in the below examples.
<html> <head> <title> My First Programme </title> </head> <body> <h1> JQuery </h1> Type something : <input type = "text"> <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script> <script> $("input").one("focus", function() { $(this).css("background-color", "yellow") }); </script> </body> </html>
$("input").one("focus", function() { $(this).css("background-color", "yellow") });
<html> <head> <title> My First Programme </title> </head> <body> <h1> JQuery </h1> Type something : <input type = "text"> <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script> <script> $("input").one("keypress blur", function() { $(this).css("background-color", "yellow") }); </script> </body> </html>
$("input").one("keypress blur", function() { $(this).css("background-color", "yellow") });
<html> <head> <title> My First Programme </title> </head> <body> <h1> JQuery </h1> Type something : <input type = "text"> <script src = "https://cdnjs.cloudflare.com/ajax/libs/JQUERY/3.3.1/jquery.min.js"> </script> <script> $("input").one( { focus : function() { $(this).css("background-color", "yellow") }, keypress : function() { $(this).css("background-color", "orange") }, blur : function() { $(this).css("background-color", "violet") } }); </script> </body> </html>
$("input").one( { focus : function() { $(this).css("background-color", "yellow") }, keypress : function() { $(this).css("background-color", "orange") }, blur : function() { $(this).css("background-color", "violet") } });
focus : function() { $(this).css("background-color", "yellow") }
keypress : function() { $(this).css("background-color", "orange") }
blur : function() { $(this).css("background-color", "violet") }