ssierral
ssierral

Reputation: 8877

How to collapse tabs in jQuery?

I'm using this Javascript script to display a list of Youtube channels related to a specific topic when this is chosen by the user, but if I click again on the name of the topic, its tab does not collapse. Instead it again shows the list of channels.

var channels=[
    { id:1, name:"Example 1", description:"", videos:[]},
    { id:2, name:"Example 2", description:"", videos:[]},
];

jQuery(function() {
    jQuery("#tabs").tabs();
    listChannels();
});

function listChannels(){
    jQuery(channels).each(function(i,channel){
        jQuery("#listOfChannels").append("<li id='channel"+channel.id+"'><span onclick='showChannel("+channel.id+")'>&gt; "+channel.name+"</span><div id='videos"+channel.id+"' class='contentChannel'></div></li>");
    });
} 

Upvotes: 0

Views: 1590

Answers (1)

krishwader
krishwader

Reputation: 11371

you could try setting collapsible: true in your tabs init. See Docs

 $( "#tabs" ).tabs({
  collapsible: true
 });

But in your situation, i feel like its better to use Accordion, a better visual representation for your channels.

AND, as @moonwave99 mentioned, its not good to use onclick attribute, especially in a loop. Read this question to know more. (Extras : Docs for event delegation ) Use click event handler which jquery provides.

$("#listOfChannels").on("click", "li" , function () {
   showChannel(this.id);
});

and, save your listChannels() from the evil clutches of onclick

jQuery(channels).each(function(i,channel){
    jQuery("#listOfChannels").append("<li id='channel"+channel.id+"'><span>&gt; "+channel.name+"</span><div id='videos"+channel.id+"' class='contentChannel'></div></li>");
});

Upvotes: 4

Related Questions