jQuery one()

Introduction

Increase the text size of a <p> element when it is clicked.

The event will only trigger once for each <p> element.

View in separate window

<!DOCTYPE html>
<html>
<head>
<script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>//  w  w w  . j  a v a2s . co m
<script>
$(document).ready(function(){
  $("p").one("click", function(){
    $(this).animate({fontSize: "+=6px"});
  });
});
</script>
</head>
<body>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<p>Click any p element to increase its text size.</p>

</body>
</html>

The one() method attaches one or more event handlers for the selected elements.

The one() method sets a function to run when the event occurs.

The event handler function attached via the one() method can only run once for each element.

$(selector).one(event,data,function)
Parameter
Optional
Description
event

Required.

one or more events to attach to the elements.
Multiple event values are separated by space.
data
Optional.
additional data to pass to the function
function
Required.
the function to run when the event occurs



PreviousNext

Related