Lahib
Lahib

Reputation: 1365

Extract text from a list of Html elements using jquery

I have an issue when trying to get the text out of a list of elements in jquery.

I have a list of elements in a varible.. What i want is to extract the text from each elements and insert it into a list of strings with comma seperation.

my code:

var names = $('input.select:checked').closest('tr').find('.citizen-name');
for (var i = 0; i < names.length; i++) {
                ???;
            }

how can i do this?

thanks for the help!

Upvotes: 0

Views: 313

Answers (3)

Gene R
Gene R

Reputation: 3744

var names = $('input.select:checked').closest('tr').find('.citizen-name');
var arr = [];
for (var i = 0; i < names.length; i++) {
   arr.push($(names[i]).text());
}
var str = arr.join();

Upvotes: 2

Jeroen Noten
Jeroen Noten

Reputation: 3624

$(names[i]).text() at the place of the question marks would work. http://api.jquery.com/text/

Upvotes: 2

Anoop Joshi P
Anoop Joshi P

Reputation: 25527

You can do,

$('input.select:checked').closest('tr').find('.citizen-name').each(function () {
    alert($(this).text());
});

OR

var names = $('input.select:checked').closest('tr').find('.citizen-name');
for (var i = 0; i < names.length; i++) {
    alert(names.eq(i).text());
}

Upvotes: 1

Related Questions