0

I have 2 anchor tags like this

<a href="#" id="1">1</a>
<a href="#" id="2" style="display:none;">2</a>

and Jquery functions like this

$(document).ready(function (){

 $('#1').click(function(){
      //code to show div 1
      //code to hide div 2
   });

   $('#2').click(function(){
       //code to show div 2
       //code to hide div 1
   });

});

Problem is when I click on "2" anchor tag, second div tag displays then when I want to again display div 1 by clicking "1" anchor tag it doesn't work. It's like after running 2nd function 1st function doesn't exist!


Updating Question with answer: I changed the selectors to like ques-1, ques-2 and if there are two selectors with same name jquery calls only first selector, so better give selector different names even if one of them is gonna be active one at a time

Cœur
  • 37,241
  • 25
  • 195
  • 267
rockvilla
  • 641
  • 8
  • 15

1 Answers1

0

If you hide anchor 1 when you click anchor 2 then you will not be able to click anchor 1 as it will not be visible. If you want to leave the area taken up by the anchor element as clickable then you must animate the opacity.

$('#1').click(function(){
    $("#1").animate({ "opacity": "1"}, 1000);
    $("#2").animate({ "opacity": "0"}, 1000);

});

$('#2').click(function(){
    $("#1").animate({ "opacity": "0"}, 1000);
    $("#2").animate({ "opacity": "1"}, 1000);
});

Check out this fiddle

Bruno
  • 5,772
  • 1
  • 26
  • 43