4

I have 4 tabs, each tab contains different data. When I click on the checkbox, the corresponding tab is shown.But when I uncheck, the tab is invisible but the contents are not invisible. How to solve this problem? My page is

<style>
    .ui-state-disabled {
    display: none;
}
</style>
    <script>
    $(function() {
  $("#tabs").tabs();
  $("#tabs").tabs("option", {
    "selected": 2,
    "disabled": [1,2,3]
  });

$( "input[type=checkbox]" ).click(function(){
    if ($(this).is(':checked')) {
     $('#tabs').tabs("enable", $(this).val());
     $('#tabs').tabs("select", $(this).val() );
    }
    else{
         $('#tabs').tabs("disable", $(this).val());
    }
});
});
</script>

.

<body>
 <div id="tabs">
  <ul>
    <li><a href="#tabs-1">Nithin</a> </li>
    <li><a href="#tabs-2">Vipin</a></li>
    <li><a href="#tabs-3">Sachin</a></li>
    <li><a href="#tabs-4">Ganguly</a></li>
  </ul>
  <div id="tabs-1">
    <p>Nithin</p>
  </div>
  <div id="tabs-2">
    <p>Vipin</p>
  </div>
   <div id="tabs-3">
    <p>Sachin</p>
  </div>
   <div id="tabs-4">
    <p>Ganguly</p>
  </div>
</div>
<input type="checkbox" name="tabs-1" value="1">tabs-1 
<input type="checkbox" name="tabs-2" value="2">tabs-2 
<input type="checkbox" name="tabs-3" value="3">tabs-3 
<input type="checkbox" name="tabs-4" value="4">tabs-4 
<br>
</body>
</html>

You can see the working code on http://jsfiddle.net/2aQ2g/12/

Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
Nithin Viswanathan
  • 3,245
  • 7
  • 39
  • 84

7 Answers7

3

Just check the else portion ... Working Demo

$(function() {
  $("#tabs").tabs();
  $("#tabs").tabs("option", {
    "selected": 2,
    "disabled": [1,2,3]
  });

$( "input[type=checkbox]" ).click(function(){
    if ($(this).is(':checked')) {
     $('#tabs').tabs("enable", $(this).val());
     $('#tabs').tabs("select", $(this).val() );
    }
    else{
        // $('#tabs').tabs("disable", $(this).val());
        $('#tabs').tabs("disable", $(this).val());
         $('#tabs').tabs("select", $(this).prev().val());
         $('#tabs').tabs("enable", $(this).prev().val());
    }
});
});
Talha
  • 18,898
  • 8
  • 49
  • 66
2

try this

$( "input[type=checkbox]" ).click(function(){
    if ($(this).is(':checked')) {
     $('#tabs').tabs("enable", $(this).val());
     $('#tabs').tabs("select", $(this).val() );
    }
    else{
         $('#tabs').tabs("disable", $(this).val());
      $("#tabs-"+$(this).val()).children().hide());
    }
});

you just disable tab.Hide the contant also

PSR
  • 39,804
  • 41
  • 111
  • 151
2

May be this could help:

$("input[type=checkbox]").click(function () {
    if ($(this).is(':checked')) {
        $('#tabs').tabs("enable", $(this).val());
        $('#tabs').tabs("select", $(this).val());
        $('[id="' + this.name + '"]').find('p').show();
    } else {
        $('#tabs').tabs("disable", $(this).val());
        $('[id="' + this.name + '"]').find('p').hide();
    }
});

Note:

No need to add a content class.

just checkout the fiddle here

Jai
  • 74,255
  • 12
  • 74
  • 103
2

Add var hhh = $("input:checked").val(); $('#tabs').tabs("enable", hhh); $('#tabs').tabs("select", hhh ); $("#tabs-"+hhh+" p").show(); to your else.

Live Demo

                $(function() {
  $("#tabs").tabs();
  $("#tabs").tabs("option", {
    "selected": 2,
    "disabled": [1,2,3]
  });

$( "input[type=checkbox]" ).click(function(){
    if ($(this).is(':checked')) {
     $('#tabs').tabs("enable", $(this).val());
     $('#tabs').tabs("select", $(this).val() );
     $("#tabs-"+$(this).val()+" p").show();
    }
    else{
         $('#tabs').tabs("disable", $(this).val());
        $("#tabs-"+$(this).val()+" p").hide();
        var hhh = $("input:checked").val();
        $('#tabs').tabs("enable", hhh);
     $('#tabs').tabs("select", hhh );
     $("#tabs-"+hhh+" p").show();
    }
});
});
MIIB
  • 1,849
  • 10
  • 21
2
    $(function() {
      $("#tabs").tabs();
      $("#tabs").tabs("option", {
        "selected": 2,
        "disabled": [1,2,3]
      });

    $( "input[type=checkbox]" ).click(function(){
        if ($(this).is(':checked')) {
         $('#tabs').tabs("enable", $(this).val());
         $('#tabs').tabs("select", $(this).val() );
        }
        else{
             $('#tabs').tabs("disable", $(this).val());
             $('#'+$(this).attr('name')).hide();
        }
    });
});
Bernie
  • 1,489
  • 1
  • 16
  • 27
2

You can check it on JSFiddle

$(function() {
  $("#tabs").tabs();
  $("#tabs").tabs("option", {
    "selected": 0,
    "disabled": [1,2,3]
  });

$( "input[type=checkbox]" ).click(function(){
    if ($(this).is(':checked')) {
     $('#tabs').tabs("enable", $(this).val());
     $('#tabs').tabs("select", $(this).val() );
    }
    else{
        $('#tabs').tabs("disable", $(this).val());
        var tab = $(this);
        $('#tabs').tabs("select", $(":checked").first().val());
        //$('#tabs').tabs("select", $(":checked").last().val());
        //It is based on you what to use. (first or last selected value)
    }
});
});

I hope this helps. Regards

X-Factor
  • 2,067
  • 14
  • 18
1

add class ui-state-disabled in else part to the element then it will display: none;

Sahil Popli
  • 1,967
  • 14
  • 21