I combined multiple jQuery scripts into one script. This script converts a <ul>
element to a <select>
box (for use in Response WebDesign). But I think this script is not well written, can someone help me clean up this script?
(function($) {
$(function() {
var $select = $('<select>').appendTo('#nav');
$('#nav li').each(function() {
var $li = $(this),
$a = $li.find('> a'),
$p = $li.parents('li'),
prefix = new Array($p.length + 1).join('-');
var $option = $('<option>')
.text(prefix + ' ' + $a.text())
.val($a.attr('href'))
.appendTo($select);
});
$(function() {
$("<option>", {
"selected": "selected",
"value": "",
"text": "Navigate to..."
}).prependTo("#nav select");
});
$(function() {
$('#nav select').bind('change', function() {
var url = $(this).val();
if (url) {
window.location = url;
}
return false;
});
});
});
})(jQuery);
Update
Thanks John, I have re-written the script tested only he does not word. Only when I use the function (function ($) { append to each function
Like this:
(function($) {
$(function() {
var select = $('<select>').appendTo('#nav');
$('#nav li').each(function() {
var li = $(this),
a = li.find('> a'),
p = li.parents('li'),
prefix = new Array (p.length + 1).join('-');
var option = $('<option>')
.text(prefix + ' ' + a.text())
.val(a.attr('href'))
.appendTo(select);
});
});
$(function() {
$("<option>", {
"selected": "selected",
"value": "",
"text": "Navigate to..."
}).prependTo("#nav select");
});
$(function() {
$('#nav select').on('change', function() {
var url = $(this).val();
if (url) {
window.location = url;
}
return false;
});
});
})(jQuery);
Update 2:
Is this one more like it?
$(function() {
$("<select />").appendTo("#nav");
$("<option />", {
"selected": "selected",
"value": "",
"text": "Navigate to..."
}).appendTo("#nav select");
$("#nav li").each(function() {
var li = $(this),
a = li.find("> a"),
p = li.parents("li"),
prefix = new Array(p.length + 1).join("-");
var option = $("<option />")
.text(prefix + " " + a.text())
.val(a.attr("href"))
.appendTo("#nav select");
});
$("#nav select").change(function() {
window.location = $(this).find("option:selected").val();
});
});
Update 3:
Thnx RobH. Zo this will be the final (good) code? I dont know which is the best they work both (update 2 and 3)
jQuery(document).ready(function($){
var options = [];
$("<select />").appendTo("#nav").append($("<option />", {
"selected": "selected",
"value": "",
"text": "Navigate to..."
}));
$("#nav li").each(function() {
var li = $(this),
a = li.find("> a"),
p = li.parents("li").length,
arr = [],
prefix = '';
arr[p] = '';
prefix = arr.join('-');
options.push($("<option />")
.text(prefix + " " + a.text())
.val(a.attr("href")));
});
$("#nav select").append(options).on('change', function() {
var url = $(this).val();
if (url) {
window.location = url;
}
return false;
});
});
Here is the html it needs to work with (Jonny Sooter asked me to post this):
<div id="nav">
<ul id="menu">
<li><a href="index.html" title="Level 1">Level 1</a></li>
<li><a href="#" title="Section 1">Section 1</a>
<ul>
<li><a href="index.html" title="Level 2">Level 2</a></li>
<li><a href="#" title="Section 2">Section 2</a>
<ul>
<li><a href="index.html" title="Level 3">Level 3</a></li>
<li><a href="index.html" title="Level 3">Level 3</a></li>
<li><a href="index.html" title="Level 3">Level 3</a></li>
<li><a href="index.html" title="Level 3">Level 3</a></li>
</ul>
</li>
<li><a href="index.html" title="Level 2">Level 2</a></li>
<li><a href="index.html" title="Level 2">Level 2</a></li>
</ul>
</li>
<li><a href="index.html" title="Level 1">Level 1</a></li>
<li><a href="index.html" title="Level 1">Level 1</a></li>
</ul>
</div>
Update 4
For the hide an show stuff i had a css solution
/* menu-view css */
#nav select { display: none; }
#nav ul { display: inline; }
@media screen and (max-width: 979px) {
/* menu-mobile-view css */
#nav ul { display: none; }
#nav select { display: inline-block; }
}
ul
to leave only theselect
showing. Since it's responsive that's probably what you'll want to do. It can be just a simple$("#menu").hide();
– Jonny Sooter Mar 28 '13 at 14:16