1

I have four links which i need to show hide there corresponding div depending on which one is clicked. Also if the same link is clicked it should hide its corresponding link. All fade fadIn.

Fiddle: http://jsfiddle.net/zangief007/mnotd7Lh/

<ul>
        <li><a id="category" href="">Catergory</a></li>
        <li><a id="style" href="">styles</a></li>
        <li><a id="brand" href="">brand</a></li>
    </ul>


    <div id="one">This is my category</div>
    <div id="two">This is styles</div>
    <div id="three">This is main brands</div>

JS:

$("#category").click(function(){
  $("#one").fadeToggle();
});

$("#style").click(function(){
  $("#two").fadeToggle();
});
$("#style").click(function(){
  $("#three").fadeToggle();
});

CSS:

#two, #three, #four{
    display:none;
}
Steve
  • 8,153
  • 9
  • 44
  • 91

2 Answers2

1

Working demo: http://jsfiddle.net/9ctfh6jc/ or this: http://jsfiddle.net/vm7oLjbd/

Try this:

$("#category").click(function(e){
    e.preventDefault();
  $("#one").fadeToggle();
});

$("#style").click(function(e){
    e.preventDefault();
  $("#two").fadeToggle();
});
$("#style").click(function(e){
    e.preventDefault();
  $("#three").fadeToggle();
});
Tats_innit
  • 33,991
  • 10
  • 71
  • 77
  • 1
    I often see people mistakenly calling `stopEvent` when `preventDefault` will do the job with fewer side effects. – Ruan Mendes Aug 24 '14 at 22:57
  • @JuanMendes progressive learning I reckon `:)` Totally agree with you! – Tats_innit Aug 24 '14 at 22:58
  • Close, only problem is it doesnt close the other div once new one is clicked? – Steve Aug 24 '14 at 22:59
  • @Steve - > lil verbose here: http://jsfiddle.net/gu5b3mk1/ but I guess above post has a `class` example which is less verbose version `:)` but this demo might do what you want : http://jsfiddle.net/gu5b3mk1/ – Tats_innit Aug 24 '14 at 23:00
1

Add a class name ("fading" for instance) to each link that you like to have this behavior and add the related div that should be toggled as an Attribute (relDiv="one" for instance). Also add a class to all related divs (class="relDiv" for instance).

So your HTML must be look like this:

<ul>
    <li><a class="fading" relDiv="one" href="">Catergory</a></li>
    <li><a class="fading" relDiv="two" href="">styles</a></li>
    <li><a class="fading" relDiv="three" href="">brand</a></li>
</ul>

<div id="one" class="relDiv">This is my category</div>
<div id="two" class="relDiv">This is styles</div>
<div id="three" class="relDiv">This is main brands</div>

And use just this few JQuery lines for as many as menu you want:

var currentDiv = 'one';
$(".fading").click(function(e){
    e.preventDefault();
    $this = $(this);
    if (currentDiv != $this.attr('relDiv')){
        $("#" + currentDiv).fadeOut( "slow", function() {
            $("#" + $this.attr('relDiv')).fadeIn();
        });
    }   
    currentDiv = $(this).attr('relDiv');
});

Check JSFiddle Demo

Moshtaf
  • 4,833
  • 2
  • 24
  • 34
  • @Moshtaf There should be links so that the page still works without JavaScript. I reckon the OP left them out for simplicity – Ruan Mendes Aug 24 '14 at 23:03
  • This is great, but only one div at a time can be open. And they need have a self closing option as well. – Steve Aug 24 '14 at 23:08
  • @JuanMendes: Absolutely you're right, but i don't know that why i don't like this `.data()` attr, it seems that don't work in all situations easily or i miss something, but i'm comfortable with `.attr()` that always works fine !! – Moshtaf Aug 24 '14 at 23:24
  • @JuanMendes: Really? if disable javascript, the divs that has `display:none'` class start hide and show if we use links? i must to check that in future. – Moshtaf Aug 24 '14 at 23:27
  • @Steve The main question you asked has been answered, you cannot post multiple requirements in a single question. Questions should be about a single problem you're facing. Otherwise, it sounds like: "Please write my code for me" – Ruan Mendes Aug 25 '14 at 12:17