Skip to content

Instantly share code, notes, and snippets.

@kingsloi
Last active March 25, 2018 04:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kingsloi/7ab83b6781636df1fbf1 to your computer and use it in GitHub Desktop.
Save kingsloi/7ab83b6781636df1fbf1 to your computer and use it in GitHub Desktop.
Generate <select> drop down boxes for Twitter Bootstrap tabs (.nav-tabs) - jsfiddle: http://jsfiddle.net/kingsloi/96ur6epu/
$(document).ready(function(){
//set the index counter
var i = 0;
//convert all .nav-tabs, except those with the class .keep-tabs
$('.nav-tabs').not( ".keep-tabs" ).each(function(){
//init variables
var $select,
c = 0,
$select = $('<select class="mobile-nav-tabs-' +i+ ' mobile-tab-headings "></select>');
//add unique class to nav-tabs, so each select works independently
$(this).addClass('nav-tabs-index-'+i);
//loop though each <li>, building each into an <option>, getting the text from the a href
$(this).children('li').each(function() {
$select.append('<option value="'+c+'">' + $(this).text() + '</option>');
c++;
});
//insert new <select> above <ul nav-tabs>
$(this).before($select);
//increase index counter
i++
});
//on changing <select> element
$('[class^=mobile-nav-tabs]').on('change', function (e) {
//get index from selected option
classArray = $(this).attr('class').split(" ");
tabIndexArray = classArray[0].split("mobile-nav-tabs-")
tabIndex = tabIndexArray[1];
//using boostrap tabs, show the selected option tab
$('.nav-tabs-index-'+tabIndex+' li a').eq($(this).val()).tab('show');
});
});
.mobile-tab-headings{
display: none;
@media only screen and (max-width:768px) {
width: 100%;
display: block;
}
+ .nav-tabs {
@media only screen and (max-width:768px) {
display: none;
}
}
}
<h2>Generate a &lt;select&gt; list for a .nav-tab</h2>
<div>
<ul id="selects" class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#home" id="home-tab" role="tab" data-toggle="tab" aria-controls="home" aria-expanded="true">Home</a></li>
<li role="presentation"><a href="#profile" role="tab" id="profile-tab" data-toggle="tab" aria-controls="profile">Profile</a></li>
<li role="presentation"><a href="#test" id="test-tab" role="tab" data-toggle="tab" aria-controls="home">Test</a></li>
</ul>
<div id="myTabContent" class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="home" aria-labelledby="home-tab">
<p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
</div>
<div role="tabpanel" class="tab-pane fade" id="profile" aria-labelledby="profile-tab">
<p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p>
</div>
<div role="tabpanel" class="tab-pane fade" id="test" aria-labelledby="test-tab">
<p>Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr.</p>
</div>
</div>
</div>
<h2>Skip gernerating a &lt;select&gt; list for a .nav-tab, using the <strong>keep-tabs</strong> class on the <strong>nav-tabs</strong> element</h2>
<div>
<ul id="keptabs" class="nav nav-tabs keep-tabs" role="tablist">
<li role="presentation" class="active"><a href="#home1" id="home-tab" role="tab" data-toggle="tab" aria-controls="home" aria-expanded="true">Home</a></li>
<li role="presentation"><a href="#profile1" role="tab" id="profile-tab" data-toggle="tab" aria-controls="profile">Profile</a></li>
<li role="presentation"><a href="#test1" id="test-tab" role="tab" data-toggle="tab" aria-controls="home">Test</a></li>
</ul>
<div id="myTabContent" class="tab-content">
<div role="tabpanel" class="tab-pane fade in active" id="home1" aria-labelledby="home-tab">
<p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
</div>
<div role="tabpanel" class="tab-pane fade" id="profile1" aria-labelledby="profile-tab">
<p>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid. Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero sint qui sapiente accusamus tattooed echo park.</p>
</div>
<div role="tabpanel" class="tab-pane fade" id="test1" aria-labelledby="test-tab">
<p>Etsy mixtape wayfarers, ethical wes anderson tofu before they sold out mcsweeney's organic lomo retro fanny pack lo-fi farm-to-table readymade. Messenger bag gentrify pitchfork tattooed craft beer, iphone skateboard locavore carles etsy salvia banksy hoodie helvetica. DIY synth PBR banksy irony. Leggings gentrify squid 8-bit cred pitchfork. Williamsburg banh mi whatever gluten-free, carles pitchfork biodiesel fixie etsy retro mlkshk vice blog. Scenester cred you probably haven't heard of them, vinyl craft beer blog stumptown. Pitchfork sustainable tofu synth chambray yr.</p>
</div>
</div>
</div>
@rijadhaxha
Copy link

Hello good job man but this scrept dosen't work in safari.

@sabipu
Copy link

sabipu commented Nov 17, 2015

need some more code for cross browser man , safari in MAC specially.

@kingsloi
Copy link
Author

@rijadhaxha / @sabipu, updated to work in Safari! Thanks for pointing it out

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment