Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums JavaScript Convert Menu to Dropdown (variation)

  • This topic is empty.
Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #37197
    rivana
    Member

    I really hope someone can help me with this. I’m trying to get the Convert Menu to Dropdown (variation) to work in my WP-theme like on https://css-tricks.com/examples/ConvertMenuToDropdown/optgroup.php

    The simple non-nested version of the script works fine, but when I try to implement this nested version something just goes awry. And yes, obviously I’m a total noob at scripting. And no, I don’t want a plugin to take away my worries. Please help anyway?

    The problem: All I get is a dropdown with the ‘selected’ value (Go to…) and no actual menu items.

    My CSS

    #access ul { display: none; }
    #access select { display: inline-block; width:90%; }

    My PHP

       

    Javscript (in menuconvert.js)

    (function($) {

    // DOM ready
    $(function() {

    // Create the dropdown base
    $("").appendTo("nav");

    // Create default option "Go to..."
    $("", {
    "selected": "selected",
    "value" : "",
    "text" : "Go to..."
    }).appendTo("nav select");

    // Populate dropdown with menu items
    $("nav > ul > li").each(function() {

    var el = $(this);

    var hasChildren = el.find("ul"),
    children = el.find("li");

    if (hasChildren.length) {

    $("", {
    "label": el.find("> a").text()
    }).appendTo("nav select");

    children.each(function() {

    $("", {
    "text": " - " + $(this).text()
    }).appendTo("optgroup:last");

    });

    } else {

    $("", {
    "value" : el.attr("href"),
    "text" : el.text()
    }).appendTo("nav select");

    }

    });

    // To make dropdown actually work
    $("nav select").change(function() {
    window.location = $(this).find("option:selected").val();
    });

    });
    })(jQuery);
    #99298
    xpy
    Participant

    If you included the menu’s code it could help…
    Is it in this form ‘nav > ul > li’ ??? Otherwise the menuconvert.js wont find a menu to copy…

    #99304
    rivana
    Member

    Thanks. :) I had to add the ‘div’ as well. Now I have the menu populated, but the links don’t want to work.

    Edit// Never mind, got it working. Looks like the script only works for pages with pretty permalinks and not numbered pages.

    On a related note – anybody know how to get the links working WITHOUT the pretty permalinks and WITH numbered pages instead? Sure, it’s not the usual way to do things, but it’s still good to know these things.

    #99305
    rivana
    Member

    The script only seems to work one level down. Does anybody know how to get it down to three or four levels?
    Also, it changes parent pages to labels which means the links for those parent pages don’t work. Since I do use my parent pages as actual pages that kinda sucks. Any solutions?

    #99398
    Mottie
    Member

    Hi Rivana!

    Can you share some HTML, it make it easier for us to help if we have something to start from and not work from scratch. Also, I’m not sure what you mean by the script changing parent pages to labels.

    #99448
    rivana
    Member

    Hey. :)

    The menu is your basic WordPress dynamic menu. In other words just a bunch of nested lists. Like thus:


    ->

    My problems are:

    1. Getting the select list more than two levels deep so that the parent/child relationship is properly reflected regarding labels and indents.
    2. The script at the moment takes the parent links and transforms them into optgroup labels. Since my parent links actually LINK to pages as well I need a way to clone them so that one link is transformed into an optgroup label while the other stays a working link. Ideally I would like the optgroup labels themselves to remain links, but I’m not sure that’s possible. Select elements are not something I’m terribly familiar with.

    Hope you can help. :)

    #102310
    jonedwards
    Participant

    I’m having a similar problem, but it’s not to do with your HTML. It’s the jQuery script that’s not working… And, like you, I don’t know how to fix it.

    #126639
    crivatf
    Member

    Try this:

    (function($) {
    // DOM ready
    $(function() {

    // Create the dropdown base
    $(““).appendTo(“nav”);

    // Create default option “Go to…”
    $(““, {
    “selected”: “selected”,
    “value” : “”,
    “text” : “Go to…”
    }).appendTo(“nav select”);

    // Populate dropdown with menu items
    $(“nav > ul > li”).each(function() {

    var level_1 = $(this);
    var level_1_hasChildren = $(this).find(“> ul”),
    level_2 = $(this).find(“> ul > li”);

    if (level_1_hasChildren.length) {

    $(““, {
    “value” : level_1.find(“> a”).attr(“href”),
    “text” : level_1.find(“> a”).text()
    }).appendTo(“nav select”);

    level_2.each(function() {

    var level_2 = $(this);
    var level_2_hasChildren = level_2.find(“> ul”),
    level_3 = level_2.find(“> ul > li”);

    if (level_2_hasChildren.length) {

    $(““, {
    “value” : $(this).find(“> a”).attr(“href”),
    “text” : ‘ — ‘ + $(this).find(“> a”).text()
    }).appendTo(“nav select”);

    level_3.each(function() {
    $(““, {
    “value” : $(this).find(“> a”).attr(“href”),
    “text” : ‘ —- ‘ + $(this).find(“> a”).text()
    }).appendTo(“nav select”);
    });

    }
    else
    {
    $(““, {
    “value” : $(this).find(“> a”).attr(“href”),
    “text” : ‘ — ‘ + $(this).find(“> a”).text()
    }).appendTo(“nav select”);
    }
    });
    }
    else
    {
    $(““, {
    “value” : $(this).find(“> a”).attr(“href”),
    “text” : $(this).find(“> a”).text()
    }).appendTo(“nav select”);
    }
    });
    // To make dropdown actually work
    $(“nav select”).change(function() {
    window.location = $(this).find(“option:selected”).val();
    });
    });
    })(jQuery);

    #126661
    Kitty Giraudel
    Participant

    @crivatf: Put 4 spaces before each line of code.

Viewing 9 posts - 1 through 9 (of 9 total)
  • The forum ‘JavaScript’ is closed to new topics and replies.