Reputation: 739
Here id dropdown fiddle http://jsfiddle.net/ym8t7Lhj/
The issue i am facing is i am having two dropdowns, if i click on one dropdown another dropdown is also opening. Can any one help me out in this.
Script
$(document).ready(function() {
$(".dropdown dt a").click(function() {
$(this).toggleClass("myclass");
$(".dropdown dd ul").toggle();
});
$(".dropdown dd ul li a").click(function() {
var text = $(this).html();
$('.dropdown dt a').toggleClass('myclass');
$(".dropdown dt a span").html(text);
$(".dropdown dd ul").hide();
$("#result").html("Selected value is: " + getSelectedValue("sample"));
});
});
Upvotes: 1
Views: 485
Reputation: 445
Try this
$(document).ready(function() {
$(".dropdown dt a").click(function() {
$(this).toggleClass("myclass");
$(this).closest('dl').find('ul').toggle();
});
$(".dropdown dd ul li a").click(function() {
var text = $(this).html();
$(this).closest('dl').find('dt a').toggleClass('myclass').find('span').html(text);
$(this).closest('ul').hide();
$("#result").html("Selected value is: " + getSelectedValue("sample"));
});
});
Upvotes: 1
Reputation: 25527
Change your code like this,
$(document).ready(function() {
$(".dropdown dt a").click(function() {
$(this).toggleClass("myclass");
$(this).closest(".dropdown").find("dd ul").toggle();
});
$(".dropdown dd ul li a").click(function() {
var text = $(this).html();
var parent = $(this).closest(".dropdown");
parent.find("dd ul").toggle();
parent.find('dt a').toggleClass('myclass');
parent.find("dt a span").html(text);
parent.find('dt ul').hide();
$("#result").html("Selected value is: " + getSelectedValue("sample"));
});
});
Note: Toggle the elements with respect to the clicked element.
Upvotes: 3
Reputation: 1282
Replace the (".dropdown dd ul").toggle();
with the following:
$(this).parent().next().find("ul").toggle();
Full code:
$(document).ready(function() {
$(".dropdown dt a").click(function() {
$(this).toggleClass("myclass");
$(this).parent().next().find("ul").toggle();
});
$(".dropdown dd ul li a").click(function() {
var text = $(this).html();
var thisRoot = $(this).closest('.dropdown');
thisRoot.find('dt a').toggleClass('myclass');
thisRoot.find("dt a span").html(text);
thisRoot.find("dd ul").hide();
$("#result").html("Selected value is: " + getSelectedValue("sample"));
});
});
Upvotes: 0
Reputation: 3832
Don't use ID for each. Only you have to remember to access onclick element via $(this)
So it will Only give you the element only..Not other with this same Class.
Upvotes: 0
Reputation: 948
You are using same selector for both drop down. Try adding ID on them then use $('dropdown1') and $('dropdown2')
[Working Code][1]
[1]: http://jsfiddle.net/ym8t7Lhj/5/
Upvotes: 0