Prev Demo
jQuery
>
How To Select Closest Element On Click Event In Jquery?
Next Demo
Write code here
J
|
B
|
A
<!DOCTYPE html> <html> <head> <title>Demo</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script> <style type="text/css"> .back { background-color:blueviolet; } </style> </head> <body> This is body <p>This is paragraph <b>1</b></p> <p>This is paragraph <i>2</i></p> <p>This is paragraph <u>3</u></p> <script> $(function () { $("body").on('click', function (e) { $(e.target).closest("p").toggleClass("back"); alert(e.target.nodeName); }); }); </script> This is body </body> </html>
Note: We DO NOT save your trial code in our database.
Output