w3resource
jquery exercises

jQuery: Double click to toggle background color of a paragraph

jQuery Fundamental - II : Exercise-72

Double click to toggle background color of a paragraph.

Sample solution :

HTML Code :

<!DOCTYPE html>
<html>
<head>
<script src="https://code.jquery.com/jquery-git.js"></script>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<p>Double click to toggle background color</p>
</body>
</html>

CSS Code :

  p {
  background: blue;
  color: white;   
  }
  p.dbl {
  background: red;
  color: white;
  }

JavaScript Code :

var pdbl = $( "p:first" );
pdbl.dblclick(function() {
pdbl.toggleClass( "dbl" );
});

See the Pen jquery-fundamental-exercise-72 by w3resource (@w3resource) on CodePen.


Contribute your code and comments through Disqus.

Previous: Display a message to the dblclick event on all paragraphs on a page.
Next: Resolve a deferred object when the user clicks a button, triggering a number of callback functions.

What is the difficulty level of this exercise?



New Content: Composer: Dependency manager for PHP, R Programming