1

I am trying to remove these "," from my HTML. I have tried .replace(",", ""), .join() and other methods but to no success.

Here is what it looks like in the console and on my page, notice the commas: enter image description here

Here is my code:

        var featuresArray = jQuery(".Features").siblings().children("p").text().replace(",", "").split("\n");

        var boom =  featuresArray.map(function (item, index) {              
          return '<li>'+item+'</li>';
          });
        var features = boom;


        printWindow.document.write("<p class='product-attribute'>"+'Features: ' + '<ul class="product-attribute-details">' + features + "</ul></p>");

How can I remove these commas from my UL ?

4
  • Notice that the way you are using replace() will only replace the first instance of the search string. see w3schools.com/jsref/jsref_replace.asp Commented Aug 29, 2018 at 13:41
  • @YVH can you dump the content of featuresArray in the question? Commented Aug 29, 2018 at 13:41
  • If you need to remove all , characters, you need to use a regular expression: .replace(/,/g, ""). Also, if you call .join(), that will add , between all of the strings that are being joined - you're looking for .join("") Commented Aug 29, 2018 at 13:43
  • @daddygames Please don't use W3Schools as a reference. Their articles are often outdated and sometimes just plain wrong. MDN is far more comprehensive and accurate: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Aug 29, 2018 at 13:44

2 Answers 2

3

The issue is because you're concatenating the array directly with the HTML string. As such, it will have toString() called on it. This is effectively doing .join(','). This is why the commas appear between the li elements, as you can see in this basic example:

var foo = ['a', 'b', 'c'];
console.log(foo.toString());

To fix this, manually call join('') on the array as you concatenate it:

printWindow.document.write('<p class="product-attribute">Features: <ul class="product-attribute-details">' + features.join('') + '</ul></p>');

Note that I made the quotes consistent in the above line, and also removed the unnecessary concatenation you were doing in a couple of places

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you ! This worked well, and I learned about the toString() behaviour!
0

Use .join('') on the array before writing it into HTML.

Since arrays aren't strings, JS will call the default .toString() function of the array when concatenating it with a string, which causes the extra commas to appear in the HTML:

var ary = [ 1, 2, 3 ];

document.write( ary );
document.write('<br>');
document.write( ary.join( '' ) );

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.