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

97
Visualizações
Check Ajax success response before printing it out

I am getting some html code with div blocks out of a Ajax call. I would like to build a if/else check on this html, more specifically on the id attribute of a div returned. I would print different results if it's either smaller or bigger than 10.

The html to be returned from the ajax call:

    <div id="10" class="names">Text</div>
    <div id="2" class="names">Text</div>
    <div id="10" class="names">Text</div>
    <div id="11" class="names">Text</div>

Ajax call

$.ajax({
  type: "POST",
  url: "https://example.com/api",
  data: data,
  cache: false,
  success: function(html) {
    //check html response for the id attribute, print html after the check 
  },
  error: {}
});

Any idea how to do that?

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

0

Something like this?

note you have two id="10"

const html = `<div id="10" class="names">Text</div>
<div id="2" class="names">Text</div>
<div id="10" class="names">Text</div>
<div id="11" class="names">Text</div>`

function success(html) {
    //check html response for the id attribute, print html after the check 
   const res = $("#content").html(html)
   .find("div")
   .map(function() {
     return $(this).attr("id")
   }).get();
   console.log(res,res.includes('10') ? 'has id 10' : 'does not have id 10')
}

success(html)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<div id="content"></div>

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