Aerodynamika
Aerodynamika

Reputation: 8413

How to select each class inside a div but separately using jQuery?

I want to select an element of each class inside a div separately. I get all the .community_node elements at once, but I need each of them separately so I can put a space between them.

How would I change my code to do that?

$('.community_cluster').click(function() {
  console.log($(this).find('.community_node').text())
});

Upvotes: 0

Views: 68

Answers (2)

Rory McCrossan
Rory McCrossan

Reputation: 337550

If you want a space between the values you could use map() to build an array of them all, then join() it by a space:

$('.community_cluster').click(function() {
  var text = $(this).find('.community_node').map(e => e.textContent).get().join(' ');
  console.log(text);
});

Upvotes: 2

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

You can iterate the divs using each(), see below code

 $('.community_cluster').click(function() {
       var text = '';
       $(this).find('.community_node').each(function(){
          text += " " + $(this).text();
       });
       console.log(text);
 });

Upvotes: 1

Related Questions