Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery .text() multiple elements same class

Tags:

jquery

text

each

I'm attempting to use .text() on multiple (unknown number of) elements on a page.

Consider:

<ul>
    <li class="myClass">element1</li>
    <li class="myClass">element2</li>
    <li class="myClass">element3</li>
</ul>
$('.myClass').text();

Will return the first element's text "element1".

I came across the following while looking around:

$('.myClass').each(function(index, obj)
{
   // do something
});

However, this simply query returns each full elements like:

<li class="myClass">element1</li>
<li class="myClass">element2</li>
<li class="myClass">element3</li>

I need to return all element's text such as:

"element1" "element2" "element3"

Thanks!

like image 754
Myles Avatar asked Feb 17 '15 10:02

Myles


People also ask

Can you select multiple elements in jQuery?

Definition and Usage. The element selector can also be used to select multiple elements. Note: Seperate each element with a comma.

Can you append multiple items jQuery?

The append method allows you to append multiple elements or text nodes to a parent node. As you can see, you can supply an unlimited amount of arguments to the append() method and it will append everything to the parent.

Can you have multiple divs with the same class?

Well you can have 2 div's with the same name provided one is an “div id” and the other is a “div class”. But you can't have the same name for two “divs” or “classes”. jatinsays: you can't have the same name for two “divs” or “classes”.

How to removeClass in jQuery?

jQuery removeClass() MethodThe removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.


3 Answers

You can use map() to retrieve a specific property of a group of elements and place them in an array:

var textValues = $('.myClass').map(function() {
    return $(this).text();
}).get();

From there you can work with the array to create the string as you need, for example, using join():

console.log(textValues.join(', ')); // = 'element1, element2, element3'

var textValues = $('.myClass').map(function() {
  return $(this).text();
}).get();

console.log(textValues.join(', ')); // = 'element1, element2, element3'
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
  <li class="myClass">element1</li>
  <li class="myClass">element2</li>
  <li class="myClass">element3</li>
</ul>

March 2020 Update

The map() call can now be made even simpler by using ES6's arrow functions.

var textValues = $('.myClass').map((i, el) => el.innerText.trim()).get();

Note, however, that this will not work in IE. You will need to use the method in the first example if you require legacy browser support.

like image 60
Rory McCrossan Avatar answered Nov 06 '22 15:11

Rory McCrossan


var x =[];
$('.myClass').each(function(index, obj)
{
  x.push($(this).text());
});

console.log(x);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul>
    <li class="myClass">element1</li>
    <li class="myClass">element2</li>
    <li class="myClass">element3</li>
</ul>
like image 38
jupeter Avatar answered Nov 06 '22 15:11

jupeter


Simply concat the text inside .each. Here is a DEMO

Here is modified jQuery:

 $(function() {
    var names = '';
    $('.myClass').each(function(index, obj) {
        names += $(this).text() +',';
    });
    alert(names);
});
like image 28
iJade Avatar answered Nov 06 '22 16:11

iJade