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

225
Views
Get "class" names of all specific divs

Whats wrong with my each function? At this way, I get all data, not just the class name:

var ClassNames = $("#list .names").each(function() {
   $(this).attr('class');
});
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

If you want to get a list of class names of items please update your code a bit:

var ClassNames = [];
$("#list .names").each(function() {
  ClassNames.push($(this).attr('class'));
});

console.log(ClassNames);
<ul id="list">
  <li class="names list-item"></li>
</ul>

about 4 years ago · Juan Pablo Isaza Report

0

You could look into .map() like this example:

var ClassNames = $("#list .names").map(function() {
   return $(this).attr('class');
}).get();

You had 2 problems, one being you are not using return inside your function. Second even with return, .each would still return the elements and not the class'

Demo

var ClassNames = $("#list .names").map(function() {
   return $(this).attr('class');
}).get();

console.log(ClassNames)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="list">
  <div class="names test1"></div>
  <div class="names test2"></div>
  <div class="names test3"></div>
  <div class="names test4"></div>
</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!