Jquery $. trim () Remove string Spaces

Source: Internet
Author: User

To delete string spaces in jquery, you can use $. trim () to remove string spaces. Next I will introduce you to anyone who needs to know about it.

It is easy to use JQuery to delete spaces before and after a string. use $. trim (StringText); // StringText is the string to delete spaces.

$. Trim (str)

Return Value: string;

Note: Remove spaces at the beginning and end of the string.

Example:

 
First look at an error code:

The Code is as follows: Copy code
<Input type = "text" name = "" id = "results" value = ""/>
Var content = $ ('# content'). val ();
If (content. trim () = '')
Alert ('null ');

No error is reported in firefox, but an error is reported in ie.

Therefore, you must write the following format:

The Code is as follows: Copy code

<Input type = "text" name = "" id = "results" value = ""/>

Var content = $ ('# results'). val ();
If ($. trim (content) = '')
Alert ('null ');

Or

Var content = $ ('# content'). val ();

If (jQuery. trim (content) = '')

Alert ('null ');

One instance

The Code is as follows: Copy code

<! Doctype html>
<Html>
<Head>
<Script src = "http://code.jquery.com/jquery-1.5.js"> </script>
</Head>
<Body>
<Button> show trim example </button>
<Script>

$ ("Button"). click (function (){
Var str = "lots of spaces before and after ";
Alert ("'" + str + "'");

Str = jquery. trim (str );
Alert ("'" + str + "'-no longer ");
});

</Script>

</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.