jquery adding greater than symbol for li which has submenu

17 Sep

I have done this for my need, you can use this code to modify to your need to insert icon or something like that in li which has another li inside it.

My html list structure

<ul class="menu">
<li>Accessories
<ul>
<li>Apparels
</li>
<li>Home items
</li>
</ul>
</li>
<li>Car Accessories
<ul>
<li>Car Speakers
</li>
<li>Car Stereos
<ul>
<li>Pioneer
</li>
<li >Sony
</li>
</ul>
</li>
<li>Car Subwoofers Amplifiers Tweeters
</li>
<li>Other Car Accessories
</li>
</ul>
</li>
</ul>

So, we have three level ul list.

Note here I’m interested in adding ‘>’ symbol for only second level ‘li’ elements which have a ‘ul’ element inside them.
use this jquery algo if your problem matches this logic

$(document).ready(function() {
//on dom ready
    $('ul.menu').children().each(function() {
//looping over children of first level ul
        $(this).children('ul').children().each(function() {
//looping over children of 2nd level ul
            $(this).children('ul').each(function() {
//check for ul child in li element and excecutes the function. each function is added just for the sake of implementing the following function
                $(this).before('<p class="lessthan">&gt;</p>');
           });
        });
    });
});

Live example
http://jsfiddle.net/pahnin/KmMC8/

Thanks for visiting

Update for this code

<?php
function getnavmenu($parent,$root){
	if($parent->count() > 0){
		$endli=true;
		if(!$root){
		echo "<li>".$parent->getname();
		$root=false;$endli=false;}
		echo "<ul>";
		foreach( $parent->children() AS $child ){
			getnavmenu($child,false);
			}
		echo "</ul>";
		if(!$endli){
			echo "</li>";
			$endli=true;}
		}
	else{
		echo "<li>".$parent."</li>";
		}
	}
if (file_exists('blocks/blocknav/nav.xml')) {
    $nav = simplexml_load_file('blocks/blocknav/nav.xml');
 
getnavmenu($nav,true);

}
?>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: