Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript
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
good
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>
<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>
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 605
1 Sergey Alexandrovich Kryukov 305
2 BillWoodruff 259
3 PIEBALDconsult 220
4 CPallini 220


Advertise | Privacy | Mobile
Web03 | 2.8.141029.1 | Last Updated 29 Oct 2012
Copyright © CodeProject, 1999-2014
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