Concatenate appended strings and remove last character

4.4k Views Asked by At

I have a list that I want to retrieve all items and append them to a paragraph. I am currently successful in doing so but it doesn't append into one string (you need to check the DOM to see it), but several instead. I have tried using .concat() to retrieve the new p but it returns an error. Also while appending I am adding a comma to separate the items, and need to remove the last one. I am using .slice(0, -1) to do so, but because they are all separate strings it does not work.

JSFiddle

HTML

<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
    <li>Five</li>
    <li>Six</li>
    <li>Seven</li>
    <li>Eight</li>
</ul>

<p>String goes here: </p>

JS/JQuery

$(document).ready(function() {
    var item = $('ul li');
    var p = $('p');

    item.each(function() {
        itemText = $(this).text();
        p.append(itemText + ", ").slice(0,-1);
    });
});
3

There are 3 best solutions below

1
On BEST ANSWER

The reason it wasnt working before is because to access the text you need to use text() and you need to do it after the iterations

p.text(p.text().slice(0, -2)); //notice -2 because you have an additional space ', '

$(document).ready(function() {
  var item = $('ul li');
  var p = $('p');

  item.each(function() {
    itemText = $(this).text();
    p.append(itemText + ", ")
  });
  p.text(p.text().slice(0, -2)); //notice -2 because you have an additional space ', '
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<ul>
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
  <li>Eight</li>
</ul>

<p>String goes here:</p>

However, a better approach would be to prepare an array and join at the end

$(document).ready(function() {
    var item = $('ul li');
    var p = $('p');
    var itemTextA = [];
    item.each(function() {
        itemTextA.push($(this).text());
    });
    p.append(itemTextA.join(', '));
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
<ul>
    <li>One</li>
    <li>Two</li>
    <li>Three</li>
    <li>Four</li>
    <li>Five</li>
    <li>Six</li>
    <li>Seven</li>
    <li>Eight</li>
</ul>

<p>String goes here: </p>

0
On

Try this

$(document).ready(function() {
    var item = $('ul li');
    var p = $('p');
    var total = item.length;

    item.each(function(index) {
        itemText = $(this).text();
        if(index === total - 1) {
           p.append(itemText);
        } else {
           p.append(itemText + ", ");
        }
    });
});

Fiddle here http://jsfiddle.net/96t0yb6d/2/

Or a modified version of above

$(document).ready(function() {
    var item = $('ul li');
    var p = $('p');
    var total = item.length;
    var seperator = ", ";

    item.each(function(index) {
        itemText = $(this).text();
        if(index === total - 1) {
           seperator = "";
        } 

        p.append(itemText + seperator);

    });
});

Fiddle here http://jsfiddle.net/96t0yb6d/1/

0
On

Try:

$(document).ready(function(){
    var item    = $('ul li');
    var p       = $('p');
    var myArray = []

    item.each(function(index) {
        itemText = $(this).text();
        if(!(index == item.length -1)) {
            myArray.push(itemText + ',');
        }
        else {
            myArray.push(itemText);
        }
    });

    p.append(myArray);
});

working example