How to find closest element to another


/ Published in: JavaScript
Save to your folder(s)



Copy this code and paste it in your HTML
  1. $(document).ready(function()
  2. {
  3. //Let's set the background color of the nearest
  4. //UL in this pseudo-menu
  5. $('li.subchild').closest('ul').css('background-color', 'red');
  6. });
  7.  
  8. // Example
  9.  
  10. /*
  11. <ul>
  12. <li>Parent Menu
  13. <ul>
  14. <li class="subchild">Child Item 1</li>
  15. <li class="subchild">Child Item 2</li>
  16. </ul>
  17. </li>
  18. </ul>
  19. */
  20.  

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.