Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

367
Visualizações
When removing jquery ui tab programatically, can I renumber the remaining ids?

I am adding and removing jquery tabs programatically, with:

function addtab(input) {
     var num_tabs = $("div#tabs ul li").length + 1;
     $("div#tabs ul").append(
     "<li class='test' title = '" + input + "'><a href='#tab" + num_tabs + "'>" + input + "</a><span class='ui-icon ui-icon-close' role='presentation'></span></li>"
     );
     $("div#tabs").append(
          "<img id='tab" + num_tabs + "' src='<%=request.getContextPath()%>/Viewer_2D'; height='100%' width='100%'/>"
     );
     $("div#tabs").tabs("option", "active", num_tabs-1);
     $("div#tabs").tabs("refresh");
}

$(function() {
     $("#tabs").on( "click", "span.ui-icon-close", function() {
          var panelId = $( this ).closest( "li" ).remove().attr( "aria-controls" );
          $( "#" + panelId ).remove();
          tabs.tabs( "refresh" );
     });
});

If I add three tabs, and then remove the last tab, there is no problem when trying to add an additional tab- the id of the new tab will be 'num_tabs'. However, if I were to add three tabs, and then remove the first tab, then the tab with id=num_tabs already exists. How can I handle this? Can I renumber the tab ids after removing tabs?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You didn't mention this in your question, but I assume there isn't a need for the ID's to be sequential. What matters is that each ID is unique.

Instead of using num_tabs to determine the number of the tab, you could simply always increment the number (do not decrement it when deleting tabs).

This can be done in multiple ways, like...

  • Use a variable that keeps track of how many tabs you've added.
  • Use jQuery's data- syntax (see .data() to store the number of the tab as a value on the tab. By doing this, you can retrieve the number on the last tab and increment it to determine the number of the next tab.
  • Parse the id of the last-tab using a regex expression to retrieve the numeric value and increment that number when adding a new tab.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda