Reputation: 19
Hi guys i have a few navigations on 1 page which look like this:
<div class="menu">
<ul>
<li>link here</li>
<li>Link Here</li>
</div>
I have 2 or 3 on one page.
I am trouble making them go into a select and options box in jquery.
It works 100% if i only have 1 on a page but because i have 2 or 3 of them on a page sometimes it only inputs the first menu into all dropdown select boxes.
Here is the jquery:
jQuery("<select />").appendTo(".menu");
jQuery("<option />", {"selected": "selected","value" : "","text" : "Navigation Menu"}).appendTo(".menu select");
jQuery(".menu ul li a").each(function() {
var el = jQuery(this);
jQuery("<option />", {"value" : el.attr("href"),"text" : el.text()}).appendTo(".menu select");
});
jQuery(".menu select").change(function() {
window.location = jQuery(this).find("option:selected").val();
});
I know its meant to look something like this but its just throwing errors.
jQuery("<select />").appendTo(".menu");
jQuery("<option />", {"selected": "selected","value" : "","text" : "Navigation Menu"}).appendTo(".menu select");
var showlis = 'ul li a';
jQuery(this + showlis).each(function() {
var el = jQuery(this);
jQuery("<option />", {"value" : el.attr("href"),"text" : el.text()}).appendTo(".menu select");
});
jQuery(".menu select").change(function() {
window.location = jQuery(this).find("option:selected").val();
});
Upvotes: 0
Views: 345
Reputation: 14469
A few things I notice:
<ul>
tag. This is invalid HTML.<select />
tag to an element found with the jQuery selector .menu
. That selector matches all elements with the menu
class. It doesn't know which element with the menu class that you're trying to append it to, so it probably just appends it to the first one every time. You will need to narrow down your selector. The best way to do this would probably be to give each navigation <div>
an id
attribute and use that as your selector.Upvotes: 0
Reputation: 66663
You need to refer to each .menu
item individually instead of referring to it via the class alone.
What the code below does is:
For each
.menu
in your DOM (page)
- Create and append a
<select>
onto it- Add a default
<option>
element- Loop through the
<li>
s and a corresponding<option>
inside the<select>
Demo: http://jsfiddle.net/5dm37/
Code:
$('.menu').each(function() {
var menu = $(this);
var sel = $("<select />").appendTo(menu);
$("<option />", {
"selected": "selected",
"value": "",
"text": "Navigation Menu"
}).appendTo(sel);
menu.find('li').each(function() {
$("<option />", {
"value": $(this).attr("href"),
"text": $(this).text()
}).appendTo(sel);
});
sel.change(function() {
alert($(this).val());
});
});
Upvotes: 3
Reputation: 1979
All of the jQuery manipulation functions outside of clone move elements.
This means that for each menu class div you need to create a new element. Creating one element will append it to the first menu class div.
EX:
$(".menu").each( function(index, element) {
//Create select and options here
$(element).append(SELECT);
});
Upvotes: 0