25

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>
2
  • is my question clear? Commented Mar 24, 2014 at 13:41
  • @Rajaprabhu Aravindasamy - I already have the answer, but thanks! Commented Mar 24, 2014 at 13:44

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

4

Use the :visible selector

if($('#element').is(':visible'))
{
    //write the code for visible
}
else
{
    // write the invisible code
}

Comments

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.

1 Comment

It's not a solution, because it hides my whole element, and user can't choose it anymoore.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.