'Trim to remove white space

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.



Solution 1:[1]

jQuery.trim() capital Q?

or $.trim()

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

Solution 2:[2]

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:[3]

or just use $.trim(str)

Solution 4:[4]

Why not try this?

html:

<p>
  a b c
</p>

js:

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

Solution 5:[5]

Try this

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Community
Solution 3 Moin Zaman
Solution 4 macio.Jun
Solution 5 Joukhar