Posted in jquery
4:22 am, June 15, 2021

jquery check if a sub element exists

this can be done with .length

HTML

<div class="menu">
<ul>
<li><a href="#!">menu</a>
<ul>
<li><a href="#!">sub1</a>
<ul>
<li><a href="#!">sub2</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>

Javascript

$(document).ready(function(){
	$('.menu > ul > li > a').click(function(e) {
		if($(this).closest('li').find('ul').length) {
    	  var message = "the 1st ul li a also contains a ul";	
          console.log(message);
          alert(message);
		}
	});
});

No Items Found.

Add Comment
Type in a Nick Name here
 
Page Views

This page has been viewed 235 times.

Search Code
Search Code by entering your search text above.
Welcome

This is my test area for webdev. I keep a collection of code snippits here, mostly for my reference. Also if i find a good site, i usually add it here.

❤👩‍💻🕹

Random Quote
I believe that success can be measured in the number of uncomfortable conversations you're willing to have.
Unknown