Posted in jquery
203
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

check if a ul contains a ul li Demo

View Demo Full Screen View Demo New Tab

check if a ul contains a ul li Code

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");
  }
});

Related Tags

No Items Found.

Add Comment
Type in a Nick Name here