How to draw a text from a span and trim the end of each span as well as separate each span with a comma, via JavaScript?
var a = $('#tegs_tagsinput').first().contents().filter(function() { return this.nodeType == 1; }).text().slice(0, -3); document.getElementById('content').innerHTML = a; <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="tegs_tagsinput" style="display:none"> <span class="tag"><span>1 </span><a href="#" title="Removing tag">x</a></span> <span class="tag"><span>2 </span><a href="#" title="Removing tag">x</a></span> <span class="tag"><span>3 </span><a href="#" title="Removing tag">x</a></span> </div> <div id="content"></div> With the help of slice , the last 3 characters were removed from the entire text, and how to remove 3 characters from each span and separate them from commas? so that in the end we get 1,2,3