Reputation: 5689
Alright. I've span
elements like below in my page.
<span class="Jens">Bill Gates</span>
<span class="noJens">Martin Reid</span>
<span class="Jens">Jeff Bezos</span>
<span class="Jens">Mark Zuckerberg</span>
<span class="noJens">Nameless Dude</span>
<span class="Jens">Jack Ma</span>
<span class="Jens">Larry Ellison</span>
I wanted to get all span values with the class name as Jens
& need to separate the values with some space or break.
I managed to get the values by giving the following code. But how to separate each entry with a space or a break?
$("span[class='Jens']").text(); //Outputs Bill GatesJeff BezosMark ZuckerbergJack MaLarry Ellison
I can achieve desired output by iterating over each elements. But Is there a way I can separate each entry with a space or \n
in a single statement like above?
Upvotes: 0
Views: 2964
Reputation: 82231
You can get them comma seprated using:
$('.Jens').map(function() { return $(this).text(); }).get().join();
For joining them with \n
pass parameter \n
in join method:
Syntax for join: array.join(separator)
$('.Jens').map(function() { return $(this).text(); }).get().join("\n")
Upvotes: 4