22

I have my two elements(setProject and setHdc). When clicked, they show other table elements. But I want to make only one group of table elements appear at the same time. for example when the user clicked on "setProject", the "setHdc" element must be hidden. and the same otherwise. Is there any way i can do it as If statment? Or is there a simpler way to do it?

<script> 
$(document).ready(function(){
  $("#setProject ").click(function(){
    $("#test1").fadeToggle("fast");
    $("#projectTable1").fadeToggle("fast");
    $("#projectTable2").fadeToggle("fast");
    $("#projectTable3").fadeToggle("fast");
  });
});
$(document).ready(function(){
  $("#setHdc").click(function(){
    $("#hdcTable1").fadeToggle("fast");
    $("#hdcTable2").fadeToggle("fast");
  });
});
</script>
PRO_gramista
  • 882
  • 1
  • 6
  • 25

3 Answers3

8

You should use

 if($(this).is(':visible')){
     doSomething();
 }else{
     doSomethingElse();
 }

the else part will only work for elements with display:none. elements that have visibility:hidden/opacity:0 will be considered visible

Oli M
  • 141
  • 3
4

Use the :visible selector

if($('#element').is(':visible'))
{
    //write the code for visible
}
else
{
    // write the invisible code
}
writeToBhuwan
  • 3,093
  • 8
  • 32
  • 63
0
$(document).ready(function(){
    $("#setProject ").click(function(){
        $("#setHdc").hide();
        $("#test1").fadeToggle("fast");
        $("#projectTable1").fadeToggle("fast");
        $("#projectTable2").fadeToggle("fast");
        $("#projectTable3").fadeToggle("fast");
    });
    $("#setHdc").click(function(){
        $("#setProject").hide();
        $("#hdcTable1").fadeToggle("fast");
        $("#hdcTable2").fadeToggle("fast");
    });
});

Just hide the opposing element on the .click event of each element.