How to replace <p> tag with a comma symbol

javascript, jquery

Solution

Use the map function, and then join the array with ', '.

$.map(
    $('<p>Copy 1</p><p>Copy 2</p><p>Copy 3</p><p>Copy 4</p>'), 
    function(val, i) { 
       return val.innerHTML; 
    }).join(', ');

Problem

I have a hidden field with a value `<p>Copy 1</p><p>Copy 2</p><p>Copy 3</p><p>Copy 4</p>`. I would like to replace the `</p>` tags with a comma symbol and remove the `<p>` tags so that the final output will be `Copy 1,Copy 2,Copy 3,Copy 4`. I tried using the JavaScript `replace('<p>','')` function, but I couldn't remove it.

Original source