Trim to remove white space

JqueryTrim

Jquery Problem Overview


jQuery trim not working. I wrote the following command to remove white space. Whats wrong in it?

var str = $('input').val();

str = jquery.trim(str);
console.log(str);

Fiddle example.

Jquery Solutions


Solution 1 - Jquery

jQuery.trim() capital Q?

or $.trim()

Edit (2021): This answer is 11 years old (2010). There are better solutions posted below.

Solution 2 - Jquery

No need for jQuery

JavaScript does have a native .trim() method.

var name = "    John Smith  ";
name = name.trim();

console.log(name); // "John Smith"

Example Here

>String.prototype.trim() > >The trim() method removes whitespace from both ends of a string. Whitespace in this context is all the whitespace characters (space, tab, no-break space, etc.) and all the line terminator characters (LF, CR, etc.).

Solution 3 - Jquery

or just use $.trim(str)

Solution 4 - Jquery

Why not try this?

html:

<p>
  a b c
</p>

js:

$("p").text().trim();

Solution 5 - Jquery

Try this

  $('input').val($('input').val().trim());

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAkhil K NambiarView Question on Stackoverflow
Solution 1 - JqueryJasonView Answer on Stackoverflow
Solution 2 - JqueryJosh CrozierView Answer on Stackoverflow
Solution 3 - JqueryMoin ZamanView Answer on Stackoverflow
Solution 4 - Jquerymacio.JunView Answer on Stackoverflow
Solution 5 - JqueryJoukharView Answer on Stackoverflow