Click here to Skip to main content
13,044,467 members (119,773 online)
Rate this:
Please Sign up or sign in to vote.
See more:
i have a menu, what i want is that when i click on parent item of menu then only subitem should display,mean i dont want to display subitem on mouseover with help of javascript . what should i do.Is there any property should i set or i have to write code for it please help.
Posted 12-Oct-12 18:39pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

The way I'd go about it would be to add content indie inside each menu item. You decide whether or not to show this content based on the class of it's parent. Then, whenever you click the parent element, you simply toggle a class name.

Here's a rough sample. The styles are pretty nasty, but it demonstrates the purpose.
Also, you shouldn't really use inline javascript on the html elements like I did. You should give them a class and then addEventListener for 'click' on any <li> element that has this className. I'll leave that as an exerise for you.

<!DOCTYPE html>
function byId(e){return document.getElementById(e);}
function newEl(tag){return document.createElement(tag);}
function newTxt(txt){return document.createTextNode(txt);}
window.addEventListener('load', mInit, false);
function mInit()
function toggleClass(element, newStr)
    if ( index == -1)
        element.className += ' '+newStr;
        if (index != 0)
            newStr = ' '+newStr;
        element.className = element.className.replace(newStr, '');
li ul
    display: none;
} ul
    display: block;
        <li onclick='toggleClass(this, "open");'>Option 1
                <li>opt 1a</li>
                <li>opt 1b</li>
        <li onclick='toggleClass(this, "open");'>Option 2</li>
        <li onclick='toggleClass(this, "open");'>Option 3
                <li>opt 1a</li>
                <li>opt 1b</li>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100