Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all white-spaces in string with jQuery

Tags:

html

jquery

this drives me nuts - Im trying to remove all white-spaces in a string, and nothing seems to work. what am i doing wrong?

This is what im trying with at the moment:

$(".unfoldedlabel a").text().replace(/ /g,'');

HTML:

<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span>
like image 265
Xeptor Avatar asked Feb 09 '23 13:02

Xeptor


1 Answers

You're not updating the text in HTML. After removing the spaces, the innerText in DOM need to be updated.

Use .text(function):

$(".unfoldedlabel a").text(function (index, oldText) {
    return oldText.replace(/\s+/g, '');
});

$(".unfoldedlabel a").text(function(i, t) {
  return t.replace(/\s+/g, '');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<span class="unfoldedlabel" colspan="6"><a>Accessories/Service & Support</a></span>
like image 148
Tushar Avatar answered Feb 11 '23 15:02

Tushar