Posted in jquery
9
4:25 am, June 21, 2021

check if a ul contains a ul li

i could not find a way to do this with just raw css so i had to use jquery /sad

HTML

<ul id='targetul'>
<li>item 1</li>
<li>item 2
<ul>
<li>nested ul item 3</li>
</ul>
</li>
<li>item 4</li>
</ul>

Javascript

$(document).ready(function(){
  // this one sets the whole list to red as that is the target
	//$("#targetul").has("ul li").css("background", "#F00");
  
  // this one checks if the target ul has a ul li and then sets that ul li to red background
  if($("#targetul").has("ul li")) { 
    $("#targetul ul li").css("background", "#F00");
  }
});
  • item 1
  • item 2
    • nested ul item 3
  • item 4

View Statistics
This Week
12
This Month
69
This Year
0

No Items Found.

Add Comment
Type in a Nick Name here
 
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.

Join me on Substack if you want me to send you a collection of the things i have done or found or read for the week. Or follow me on twitter if you prefer, i dont post much but i probably should!

❤👩‍💻🕹

Random Quote


Me