<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
<div class="row">
<div class="col s12" id="tabs">
<ul class="tabs">
<li class="tab col s3" id="first"><a class="pestanas active" id="Tab1" href="#test1">Nombre Actual</a><input style="text-align: center; color: pink;" type="hidden" name="tab1" id ="inputTab1" value="" placeholder="Nuevo Nombre"/></li>
<li class="tab col s3"><a id="Tab2" href="#test2">New Tab<input type="hidden" name="tab2" value="New Tab" /></a></li>
<li class="tab col s3" id="tabControl"><a id="control" onClick="agregar()" href="javascript:void(0)">+</a></li>
</ul>
</div>
<div id="test1" class="col s12">
texto de la primera pestaña</div>
<div id="test2" class="col s12">Texto de la segunda pestaña</div>
</div>
JavaScript
//Obtengo el dobre clic en la tab
$("#Tab1").dblclick(function () {
var texto = $("#Tab1").text();
$("#Tab1").remove();
$("#inputTab1").attr("type", "text");
$("#inputTab1").val(texto);
$("#inputTab1").focus();
});
//Detecto el keyup para ver cuando den enter
$('#inputTab1').on('keyup', function(e) {
if(e.keyCode === 13) {
//Valor del imput
var input1 = $(this).val();
//Agrego el enlace antes del imput dentro de la li
$("#inputTab1").before('<a class="active" id="Tab1" href="#test1">' + input1 + '</a>');
//Limpio el campo
$("#inputTab1").val("");
//Oculto el campo
$("#inputTab1").attr("type", "hidden");
//Reinicio el monitoreo del doble clic o siclo
$("#Tab1").dblclick(function () {
//Ya sabes el resto
var texto = $("#Tab1").text();
$("#Tab1").remove();
$("#inputTab1").attr("type", "text");
$("#inputTab1").val(texto);
$("#inputTab1").focus();
});
}
});
function agregar(){
$(".pestanas").each(function(){
$(this).removeClass('active');
});
$(".contenidos").each(function(){
$(this).removeClass('active').css('display','none');
})
var id = Math.floor((Math.random() * 10000) + 1);
var pesta = '<li class="tab col s3"><a class="pestanas active" id="Tab'+id+'" href="#test'+id+'">Nueva Tab<input type="hidden" name="tab2" value="New Tab" /></a></li>';
var conte = '<div id="test'+id+'" class="col s12 contenidos active">Testo pestaña nueva '+id+'</div>';
$("#tabControl").before(pesta);
$("#tabs").after(conte);
}
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.
Fiddle Pages
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.
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!
Get Fiddle Pages with PRO
Your fiddles accessible under a custom domain and a vanity path - the simplest hosting for your personal project.