Jul 9, 2008

Web Page Tree Menu: Style Sheet - Javascript

Free Web Hosting, No Ads > CONTRIBUTE > The Internet > Web Design

free web hosting

Web Page Tree Menu: Style Sheet - Javascript

saga
I got bored and lazy working on the things that I should do... so I end up creating this tree menu for anyone to use if they find it interesting.

Well the idea is to create a tree menu in a webage. Instead of those linear menu it would be better to add more dynamic to your webpage with a tree menu. Others use image rollovers. As expecte the tree menu should behave like the one in Windows Explorer. When you click a node depending on its state it will pull down or pull up its chlid nodes.

I have achieved by exploiting one style sheet property, display. Basically this style sheet property has 3 values: inline, block , list-item and none. We are more interested in the value block and none. The block value displays the content in block form as opposed to inline in which it will display elements in one line. The none value hides the elements but it is not the same witht he visibility: hidden which hides the element but still takes up space in your page.

The javascript part
There are 2 functions for this tree menu to work. The first function is the initNodes() which is used in <body onload="initNodes()". What this function do is to set the all the nodes display to none except the root node so that when the page load only the root node is shown and all the child nodes are hidden. The second function clickNode(id) is responsible in hiding or showing the child node of the node that is being clicked. It takes one function parameter which is the element id of its child node. It uses the chlid node id to set the display to block or none depending on the current state. If the child node current display is none then it will change it to block and vice versa.
CODE
<head>
        &lt;script type="text/javascript">
        <!--
            function initNodes(){
            var uls = document.getElementsByTagName("ul");
            var i;
                for(i = 0; i < uls.length; i++)
                    if(uls[i].id != "root")
                        uls[i].style.display = "none";
            }

            function clickNode(id){
            var node = document.getElementById(id);
                if(node.style.display == "none")
                    node.style.display = "block";
                else
                    node.style.display = "none";
                return false;
            }
        -->
        </script>
    </head>



The HTML Code
I used <ul></ul> (unordered list) tag becuase you dont need to indent it. If you want more styles you could use <div></div> container tag and you will have to indent it yourself depending on the level of the nodes. But I suggest you use the <ul></ul>, just apply some styles on it to fit your desired look like putting some icons which will change when the its child node of a node is hidden or not. This can be achieved by putting an image just before the nodes name and using the javascript rollover technique to change the image source. Like in windows explorer when you click a node the image changes from a normal folder to an open folder or vice versa. But for the simplicty of this discussion I did not include it. I might loss interest and not be able to finnish this one.

CODE
<body onload="initNodes()">
    Example of a tree menu
    <ul id="root">
        <li><a href="" onclick="return clickNode('r1n1')">Root 1</a>
            <ul id="r1n1">
                <li>Node 1
                </li>
                <li>Node 2
                </li>
                <li>Node 3
                </li>
            </ul>
        </li>
        <li><a href="" onclick="return clickNode('r2n1')">Root 2</a>
            <ul id="r2n1">
                <li><a href="" onclick="return clickNode('r2n1n1')">Node 1</a>
                    <ul id="r2n1n1">
                        <li>Node 1
                        </li>
                        <li>Node 2
                        </li>
                        <li>Node 3
                        </li>
                        <li>Node 4
                        </li>
                        <li>Node 5
                        </li>
                        <li>Node 6
                        </li>
                        <li>Node 7
                        </li>
                        <li>Node 8
                        </li>
                        <li>Node 9
                        </li>
                    </ul>
                </li>
                <li>Node 2
                </li>
                <li>Node 3
                </li>
                <li><a href="" onclick="return clickNode('r2n4n1')">Node 4</a>
                    <ul id="r2n4n1">
                        <li>Node 1
                        </li>
                        <li>Node 2
                        </li>
                        <li><a href="" onclick="return clickNode('r2n4n1n3')">Node 3</a>
                            <ul id="r2n4n1n3">
                                <li>Node 1
                                </li>
                                <li>Node 2
                                </li>
                                <li>Node 3
                                </li>
                                <li>Node 4
                                </li>
                            </ul>
                        </li>
                    </ul>
                </li>
                <li>Node 5
                </li>
            </ul>
        </li>
    </ul>
    using style sheet and javascript.
    </body>


If you want to see how it works, I have created a working tree page as an example. Just click here for the test page of a tree menu.

Maybe next time I will add the icons on each node which will show the state of the nodes. But for now that will be all coz Im getting bored again.

 

 

 


Reply

iGuest
*************GOODONE****************
Web Page Tree Menu: Style Sheet - Javascript

Nice and simple way to get a tree control ...Keep it up

-reply by madhan

Reply

iGuest
Thanks
Web Page Tree Menu: Style Sheet - Javascript

Nice code. This is the exact thing I was looking for. Thank you for making it clean and easy to recycle. You've saved me a bunch of time and helped me organize a massive hierarchy of files that I have.

-reply by William

Reply

iGuest
Single Node Expansion
Web Page Tree Menu: Style Sheet - Javascript

How could we adjust the code so that only single node could be expanded at one time?

-question by Dan Kunes

Reply

iGuest
How to make menu stay explore on page refresh?
Web Page Tree Menu: Style Sheet - Javascript

How to make menu stay explore on page refresh?

The problem comes when we need to do explored menu, when one of menu item has pushed and the page refresh.

This is important, when visitor has choosen meniu item and gets it content, but the meniu has colapsed and it must be explore again...

Everythink else is great!

-feedback by Non commercial

Reply

iGuest
Aligning child menu
Web Page Tree Menu: Style Sheet - Javascript

I had created a Vertical parent menu named as Products and its child menu are rice and paddy. I had aligned the parent menu but the child could not able to align the rice and paddy resply.Menu's are created through coffe cup menu maker and edited through dreamweaver. So please if any body have the code reply me soon ASAP.

-question by nambi

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.
Confirm Code:

Recent Queries:-
  1. menu styles in javascript - 11.14 hr back.
  2. how do you make up a menu sheet - 12.33 hr back.
  3. javascript to display the menu in a tree form - 24.87 hr back.
  4. vertical tree hierarchy in javascript - 28.61 hr back.
  5. tree menu style - 32.89 hr back.
  6. how to show elements in tree form in jsp - 33.76 hr back.
  7. javascript change menu state - 46.04 hr back.
Similar Topics

Keywords : web, page, tree, menu, style, sheet, javascript

  1. Javascript Conflict
    JScrollpane & Lightwindow / Lightbox (1)
  2. Add A Javascript Feedback Form On Your Web Page
    This is how to do it (6)
    So i was looking for a feedback form in javascript and i couldn't find one so ive made one for
    you guys here we go! So this feedback form will ask for name, emails address and their comments
    First of all add this code in between the and tags CODE <script
    name="JavaScript"> <!-- function SendEmail() {         var toaddy =
    'name@domain.com';         var subject = 'JS Form Submission';         var mailer =
    'mailto:' + toaddy + '?subject=' + subject + '&body=' +
    'Name%20is\....
  3. Accessing Mysql From Javascript
    Javascript to communicate with database (4)
    I found a nice program which allows you to put quizzes on your web site. You can design your own
    questions, set the layout of your page, and there is also a possibility of having the results
    emailed to you. It further offers a facility to store the results and all that, all you have to do
    is put a link in the program, telling it which page takes care of that. However, when I look at the
    HTML code the program generates, I see that most of it is written in Javascript. Obviously, it also
    uses variable names, but as I don't know any Java, I was wondering if those variab....
  4. Rolling Menu
    (2)
    Hi ,does anybody know some code for make menu like at picture? When you click at some name at that
    menu it will redirect you at site what you will set. Some codes..Please i need it fast /smile.gif"
    style="vertical-align:middle" emoid=":)" border="0" alt="smile.gif" /> Thanks for help.....
  5. No Frames, But Still A Nice Menu?
    (11)
    Hi, So apparently frames are not done anymore. The nice thing about frames was, that you could have
    a nice menu, which was always positioned at the same spot. I am currently constructing a Joomla
    page, as I am working on a website that has to be eddited by people that don't know too much
    about html. Is there any way to get a menu that does not move while scrolling down? Please help.
    Marco ....
  6. Help Required Regarding Adding Javascript In A Web
    Info. (2)
    Hey...I have a java code of RSS feeds.When i put it in my website , on front page,and when i
    preview,there is blank place (where news should be) and there is a yellow bar on internet
    explorer..there is a message.. "To Help protect your security,Internet explorer has resticted this
    file from showing active content that could access your computer.Click here for more options" And
    when i click to that bar, there is option "ALOW BLOCKED CONTENT" THen it shows those news. I am
    very upset.Because i dont want that bar.Can you please help me by telling the way , that there comes....
  7. How To Put Javascript And Css In Dreamweaver
    (9)
    HI, If anybody worked with dreamweaver, and succesfully used JS codes and CSS in their website,
    please reply and tell me how to, cause when I try too put Javascripts it won't work....I also
    tried to put an textarea with scrollbars in a table...put in design view the content won't stay
    in the widths and heights I typt in...can someone help? thanks ....
  8. Free Dhtml Menu That Reads A Database
    Anyone know one? (7)
    Hey, anybody know a DHTML menu that reads info from a database? for people who don't know, just
    like windows' "start menu" with the popup menus I want to mod it so that in phpnuke I can edit
    it admin panel end. I would like it to be vertical. Do not use note tags because they are only for
    admins and moderators. ....
  9. Help With Javascript
    (3)
    Anybody want to point me to a good javascript tutorial. Thanks.....
  10. Excellent Site For Javascript Tools
    (8)
    This is the best source for Javascript tools. http://www.codebrain.com/ ....
  11. Create Xp-style Icons Using Illustrator Or Freehan
    (2)
    In this third and last article of our vector graphic series, you'll use your knowledge of faux
    3d vector graphics as we walk step-by-step through a real-life, practical application that I think
    you'll find extremely useful. You'll learn how to make your own Windows XP-style artwork,
    which you may end up using for Website graphics, software application icons, or in other projects.
    Note: I'll be making my diagrams and commands using Adobe Illustrator 8, but those who use
    Freehand should be able to follow along just as easily. Let's start by deconstru....
  12. Javascript resources?
    ie, a book. (-1)


      Looking for web, page, tree, menu, style, sheet, javascript

Searching Video's for web, page, tree, menu, style, sheet, javascript
advertisement



Web Page Tree Menu: Style Sheet - Javascript



 

 

 

 

ADD REPLY / Got an Opinion! Remove these ADs! RAPID SEARCH! Free Web Hosting [X]
Express your Opinions, Thoughts or Contribute more info. to help others.
Ask your Doubts & Queries to get answers, So that "Together We can help others!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE