$(function() {
// Setup tabs
for (var i = 1; i < 21; i++) {
var tabText = "Tab #"+i;
if (i == 17) {
tabText = "Long Tab Name to Ruin Your Day";
}
$("ul.parent").append('<li><a href="#">'+tabText+'</a></li>');
}
// Arbitrary actions on tabs. This is to test that events get carried to new tabs
$("ul.parent > li > a").click(function(e) {
e.preventDefault();
if ($(this).find("ul.children").length == 0) {
$("ul.parent a.selected").removeClass("selected");
$(this).addClass("selected");
$(".output").text("Clicked: " + $(this).text());
}
});
var numTabs = $("ul.parent > li").length;
var maxWidth = $("ul.parent").width(); // Upper limit
var widthTally = 0;
var moreEl = null;
function addTabToEl(tab, el) {
tab.click(function() {
$(".has-children > a").addClass("selected").html("▾ "+tab.find("a").text());
});
tab.appendTo(el);
}
$("ul.parent > li").each(function(index) {
// Width of element = width + margin + padding + border
widthTally += $(this).width() +
parseInt($(this).css("margin-left").replace("px", "")) +
parseInt($(this).css("margin-right").replace("px", "")) +
parseInt($(this).css("padding-left").replace("px", "")) +
parseInt($(this).css("padding-right").replace("px", "")) +
parseInt($(this).css("border-left-width").replace("px", "")) +
parseInt($(this).css("border-right-width").replace("px", ""));
if (widthTally > maxWidth) {
if (!moreEl) {
// TODO - "More" could take up more width than current tab, in which case
// we need to loop back through elements and pick the one that will allow
// us to modify the text without going over the width restriction.
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.