0

This is a horizontal menu using traditional JavaScript.

function createcssmenu()
{
    var ultags = document.getElementById("navmenu").getElementsByTagName("ul");
    for (var t = 0; t < ultags.length; t++)
    {
        ultags[t].style.top = ultags[t].parentNode.offsetHeight -1 + "px";
        ultags[t].parentNode.onmouseover = function()
        {
            this.style.zIndex = 100;
            this.getElementsByTagName("ul")[0].style.visibility = "visible";
            this.getElementsByTagName("ul")[0].style.zIndex = 0;
        }
        ultags[t].parentNode.onmouseout = function()
        {
            this.style.zIndex = 0;
            this.getElementsByTagName("ul")[0].style.visibility = "hidden";
            this.getElementsByTagName("ul")[0].style.zIndex = 100;
        }
    }
}

if (window.addEventListener)
    window.addEventListener("load", createcssmenu, false);
else if (window.attachEvent)
    window.attachEvent("onload", createcssmenu);

I need to re-write it using jQuery syntax.

This is where I came to:

$(document).ready(function ()
{
    $('#navmenu ul').css('top', $('#navmenu ul').parent().height() - 1 + "px");

    $('#navmenu ul').parent().bind('mouseover', function ()
    {
        $(this).css('z-index', 100);
        $('#navmenu ul').css({ 'visibility': 'visible', 'z-index': 0 });
    });

    $('#navmenu ul').parent().bind('mouseout', function ()
    {
        $(this).css('z-index', 0);
        $('#navmenu ul').css({ 'visibility': 'hidden', 'z-index': 100 });
    });
});

It doesn't work correct.

I still have trouble with this.getElementsByTagName("ul")[0] line.

Look at JSfiddle http://jsfiddle.net/sublay/HCajr/

It should work a normal menu.

Thank you!

Related question JavaScript to jQuery syntax Still need help on Converting

2 Answers 2

1

I think this is what you want FIDDLE

$(document).ready(function ()
{
    $('#navmenu ul').css('top', $('#navmenu ul').parent().height() - 1 + "px");
    $('#navmenu ul').each(function(){
        $(this).css('top', $(this).parent().height() - 1 + "px")
    });

    $('#navmenu ul').parent().bind('mouseover', function ()
    {
        $(this).css('z-index', 100);
        $('ul',this).css({ 'visibility': 'visible', 'z-index': 0 });
    });

    $('#navmenu ul').parent().bind('mouseout', function ()
    {
        $(this).css('z-index', 0);
         $('ul',this).css({ 'visibility': 'hidden', 'z-index': 100 });
    });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Instead of trying to rewrite the above javascript, you could always just simplify it.

Have a look at this fiddle - http://jsfiddle.net/DeHQ5/

$(document).ready(function () {
    $('#navmenu ul').css('top', $('#navmenu ul').parent().height() - 1 + "px");

    $('#navmenu > li').bind('mouseover', function () {
        $(this).children('ul').css({
            'visibility': 'visible',
            'z-index': 0
        });
    });

    $('#navmenu > li').bind('mouseout', function () {
        $(this).children('ul').css({
            'visibility': 'hidden',
            'z-index': 100
        });
    });
});

The main change is the initial sibling selector #navmenu > li.

1 Comment

As long you are using a version of jquery that is compatible with ie7 then they will be.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.