Click here to Skip to main content
15,886,676 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

1 solution

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.


HTML
<!DOCTYPE html>
<html>
<head>
<script>
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)
{
    index=element.className.indexOf(newStr);
    if ( index == -1)
        element.className += ' '+newStr;
    else
    {
        if (index != 0)
            newStr = ' '+newStr;
        element.className = element.className.replace(newStr, '');
    }
}

</script>
<style>
li ul
{
    display: none;
}
li.open ul
{
    display: block;
}
</style>
</head>
<body>
    <ul>
        <li onclick='toggleClass(this, "open");'>Option 1
            <ul>
                <li>opt 1a</li>
                <li>opt 1b</li>
            </ul>
        </li>
        <li onclick='toggleClass(this, "open");'>Option 2</li>
        <li onclick='toggleClass(this, "open");'>Option 3
            <ul>
                <li>opt 1a</li>
                <li>opt 1b</li>
            </ul>
        </li>
    </ul>
</body>
</html>
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900