Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

98
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!