Home › Forums › JavaScript › Convert Menu to Dropdown (variation)
- This topic is empty.
-
AuthorPosts
-
March 17, 2012 at 12:29 pm #37197
rivana
MemberI 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);
March 17, 2012 at 3:02 pm #99298xpy
ParticipantIf 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…March 17, 2012 at 5:17 pm #99304rivana
MemberThanks. :) 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.
March 17, 2012 at 6:46 pm #99305rivana
MemberThe 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?March 19, 2012 at 7:51 am #99398Mottie
MemberHi 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.
March 19, 2012 at 7:11 pm #99448rivana
MemberHey. :)
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. :)
May 4, 2012 at 9:49 am #102310jonedwards
ParticipantI’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.
March 1, 2013 at 8:14 pm #126639crivatf
MemberTry 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);March 2, 2013 at 3:38 am #126661Kitty Giraudel
Participant@crivatf: Put 4 spaces before each line of code.
-
AuthorPosts
- The forum ‘JavaScript’ is closed to new topics and replies.