Jquery Closest Example - Jquery

How to code jquery closest method?

Snippet Code


  
Rate this page :
  [ 0 votes]

Jquery closest code is used to return the first ancestor or a parent from the selected element. The simple jquery closest code is given below.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> <style> li { margin: 3px; padding: 3px; background: #EEEEEE; } li.hilight { background: red; } </style> <span> <ul> <li><b>This is first li</b></li> <li><b>This is second li</b></li> <li><b>This is third li</b></li> <li><b>This is fourth li</b></li> </ul> </span> <script> $('li').click(function (event) { $( event.target ).closest("li").toggleClass("hilight"); }); </script>

Tags


Ask Questions

Ask Question