Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use jQuery's trim() function

As discussed in many questions on stack - IE 8 wont accept .trim(), but the jQuery framework takes care of that.

I don't know how to translate my function to use that version of trim (I thought I was already using jQuery), could someone advise? Here is my code:

$('input').val(function(index, val){
    return val.replace('Please Select', '').trim();
});

This is designed to replace the string with nothing.

I've tried:

$('input').val(function(index, val){
    return val.replace('Please Select', '')$.trim();
});

but that was no good.

like image 469
Gideon Avatar asked Dec 03 '12 10:12

Gideon


People also ask

What is the use of trim in jQuery?

trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

How remove all spaces from a string in jQuery?

Answer: Use the jQuery $. trim() function You can use the jQuery $. trim() function to remove all the spaces (including non-breaking spaces), newlines, and tabs from the beginning and end of the specified string.

What do trim () do?

The trim() method removes whitespace from both ends of a string.

What is trim Ajax?

The trim() method removes whitespace from both sides of a string. The trim() method does not change the original string.


3 Answers

$.trim(val.replace('Please Select', ''))

http://api.jquery.com/jQuery.trim/

like image 149
Tetaxa Avatar answered Sep 22 '22 15:09

Tetaxa


IE8 doesn't have a native trim method, generally, I just augment the prototype:

if (!String.prototype.trim)
{
    String.prototype.trim = function()
    {
        return this.replace(/^\s+|\s+$/g,'');
    };
}

This is the shortest regex to trim a string, but I have heard it say that .replace(/^\s\s*/,'').replace(/\s*\s$/,'') is (marginally) faster... the choice is yours

If you really want to use jQuery for this, of course, you'll have to make the target string the context of the called method ($.trim calls the method on $ === the jQuery object), so make the String a jQ object:

$('  foo bar  ').trim();//returns "foo bar"
//compared to augmented prototype:
'   foo bar  '.trim();//returns "foo bar"

The benefit of an augmented prototype is that you don't have the additional overhead of creating a new jQuery object, whereas using the prototype-approach relies on JS to wrap the string in a native String object and apply the method to that. Basically, it's the same operation, but it should be a marginally more efficient, because jQuery does a bunch of checks to any string passed to the jQuery constructor ($())

like image 43
Elias Van Ootegem Avatar answered Sep 22 '22 15:09

Elias Van Ootegem


Try this:

$.trim(val.replace('Please Select', ''));

Here's the Trim() entry in the documentation.

like image 40
Rory McCrossan Avatar answered Sep 23 '22 15:09

Rory McCrossan