jQuery toggle() between colors

Introduction

Toggle between colors when clicking on a <p> element:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.8/jquery.min.js">
</script>//from  w  w w .j ava  2  s . c  om
<script>
$(document).ready(function(){
  $("p").toggle(
    function(){$("p").css({"color": "red"});},
    function(){$("p").css({"color": "blue"});},
    function(){$("p").css({"color": "green"});
  });
});
</script>
</head>
<body>
<p style="font-size:40px">Click me.</p>
</body>
</html>

The toggle() method was deprecated in jQuery version 1.8, and removed in version 1.9.

The toggle() method toggles two or more functions for the click event.

The first click fires the first function.

The second click fires the second function.

$(selector).toggle(function)
Parameter Optional Description
function Required. A function to run every time the selected element is clicked



PreviousNext

Related