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://ajax.googleapis.com/ajax/libs/jquery/3.7.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")
});1.png)
<html>
<head>
<title> My First Programme </title>
</head>
<body>
<h1> JQuery </h1>
Type something : <input type = "text">
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.7.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")
});2.png)
<html>
<head>
<title> My First Programme </title>
</head>
<body>
<h1> JQuery </h1>
Type something : <input type = "text">
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.7.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")
}3.png)