Question: I think `trim()` doesn't store the trimmed value. 

I apply trim to the result before the user submits their string and it works, it displays properly by removing the extra space. But if I retrieve the trimmed value back, it gives me the original of user's input, it doesn't remove space.

Before I retrieve the user input from the input field, I added `trim()`:

```
input = $("<input type='text'>").val(txt);
input.trim()

```

It doesn't work.

Answer: 
You should assign it back otherwise it's meaningless.

```
txt = txt.trim();
input = $("<input type='text'").val(txt);

```

Note: the above is using the native JavaScript `trim()` method, assuming `txt` is plain string. If your site should support IE8 and below, refer to [this answer](https://stackoverflow.com/a/498995/447356) to see how to add such support.

Update, based on OP comments. Turns out the request is to truncate all inner spaces, same way HTML is doing. To achieve this, plus better support for the trim change this line:

```
var text = $(this).text();

```

To those three:

```
var text = $.trim($(this).text());
while (text.indexOf("  ") > 0)
    text = text.replace("  ", " ");

```

[Updated fiddle](http://jsfiddle.net/bq3tN/6/).
The `trim` method is a static method, i.e. you access it from the jQuery object but it acts on the data that you send into it, and it returns the result.

To get the data from the input, trim it, and put it back:

```
input = $("<input type='text'>").val(txt);
input.val($.trim(input.val()))

```

You probably want to just trim it before putting it in the input in the first place:

```
input = $("<input type='text'>").val($.trim(txt));

```