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

113
Visualizações
.append keeps appending even though if statement is false

Not sure why, but when clicking my button, my code keeps appending even though it appears the if statement is false after the first append?

In line 9, I am checking to see if any .tab class has an attr that does not equal my variable sysId. No matter what I do, it keeps appending. I want it to only append once IF a .tab doesn't have an attribute of data-sysid= sysid already in place.

var recordHtml = '<div class="tab-content-record" data-sysid="">This is a record opened 
from the list.</div>';

// Open Record Tab
$(".tab-content-list-button").click(function () {
  var sysId = $(this).data('sysid');
  var title = $(this).data('title');
  var tabHtml = '<div class="tab record" data-sysid="' + sysId + '"><div class="tab-title">' + title + '</div><div class="close-tab">X</div></div>';

  if ($('.tab').attr('data-sysid') != sysId) {
    $(".tab-list").append(tabHtml);
  }

  //$(".tab-list").append(tabHtml);

})

Here is my fiddle that shows it working with the HTML & CSS: https://jsfiddle.net/fju60bca/1/

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

0

You have many tab elements on the page, so the following statement won't return the attribute you are looking for:

$('.tab').attr('data-sysid')

You need to check each tab element to see if the one you are looking for exists:

let buttonExists = false; 

$("div.tab").each(function(index, tabElem) 
{
    if ($(tabElem).data('sysid') === sysId)
        buttonExists = true; 
});
  

if (!buttonExists) {
    $(".tab-list").append(tabHtml);
}
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