ok, So I have multiple divs(7) which I want to toggle for show hide. If one div is open rest should hide and when I open a new div the others should hide. I have been able to accomplish this with the below piece of jquery
function showDivs() {
$(".head").click(function() {
$(".Content").hide();
$(this).next().fadeIn("slow");
})
}
where .head
is the header for each div and .Content
is the class for divs. I have got it working perfectly, by calling showDivs()
from .head()
Now the question is that on the left hand side of my page, I have ul li
set. I have 7 li
items, that correspond to 7 divs. I mean on click of first li
the corresponding div
should open up and the others should hide, and on click of 2nd li
the 2nd div
should open up and the others hide.
Does anybody have an idea how to make these divs show hide on the action of li
items on left. I know I have to pass parameters for showDivs()
, but don't know how?
help is appreciated
I believe this is where .index()
comes into play:
$(function() {
$('ul li').each(function() {
$(this).click(function(e) {
var i = $(this).index();
$('div').hide();
$('div:eq('+i+')').show();
});
});
});
That's a pretty basic markup but I'm sure you can work out how to get it working with your code. Hope I helped!
EDIT: After having seen your fiddle I think i worked out exactly what you want:
$(function() {
$('ul li').each(function() {
$(this).click(function(e){
e.preventDefault();
var i = $(this).index();
$('.content').hide();
$('.head:eq('+i+')').next().show();
});
});
});
Take a look at the fiddle: http://jsfiddle.net/DTcGD/25/