On and Off Method tutorial in jQuery
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>On and Off Method jQuery</title>
<style>
body {
font-family: arial
}
#box {
padding: 10px;
border: 1px solid #000;
}
.boxbg {
background: pink;
}
</style>
<body>
<h1>jQuery On() Methods</h1>
<div id="box">
<h2>Test Box</h2>
<p>Lorem ipsum dolor sit amet consectetur, adipisicing elit. Quaerat quisquam quas error culpa amet doloremque
porro eum non. Ex tempora expedita officia minus temporibus, animi earum et odit corporis placeat!</p>
</div>
<br>
<button>Remove Event</button>
<script src="js/jquery-3.7.1.min.js"></script>
<script>
$(document).ready(function () {
//Below is only one event and one function
$("#box").on("click", function () {
$(this).css("background", "orange");
});
//Below is multiple events and same one function
$("#box").on("mouseover mouseout", function () {
$(this).toggleClass("boxbg");
});
//Main Use of "On()" method is as shown below :
//Below is multiple events and multiple functions
$("#box").on({
"click": function () {
$(this).css("background", "orange");
},
"mouseover": function () {
$(this).css("background", "pink");
},
"mouseout": function () {
$(this).css("background", "lightblue");
}
});
//"off()" method is used to remove events
//Here below, we have removed "mouseover" and "mouseout" events of selector "box"
$("button").click(function () {
$("#box").off("mouseover mouseout");
});
});
</script>
</body>
</html>



Comments
Post a Comment