0

I am collecting the text from several elements like this:-

$('#element').find('p').text();

However this returns the text of all the 'p' elements added together without any form of delimiting. e.g. If each 'p' element contains a musician/artist name I get the following when collecting them all:-

The BeatlesRadioheadBritney Spears

What I want to do is insert a space (or comma) between each artist so that the above list looks like:-

The Beatles Radiohead Britney Spears

I would imagine the .map/.split/.join function will do the trick but my inexperience with jQuery is holding me back here.

Any ideas?

1

1 Answer 1

3

You are right about using .map along with .join.

Try something like this:

var names = $('#element').find('p').map(function(){
   return $(this).text();
}).get().join(',');
console.log(names); //The Beatles,Radiohead,Britney Spears
1
  • Perfect! Many thanks, it works beautifully. I was definitely along the right lines, but as I said my inexperience with the syntax left me confusing myself.
    – gordyr
    Oct 25, 2011 at 14:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.