Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Safari not like the .trim() in JQUERY?

This works fine in FireFox: $("#listname").val().trim()

But in safari it errors: $("#listname").val().trim() while this does work, $("#listname").val()

Why is that?

like image 295
AnApprentice Avatar asked Jan 28 '26 16:01

AnApprentice


1 Answers

There's no intrinsic trim function on strings. jQuery does define $.trim, but you use it like this:

$.trim($("#listname").val())

E.g., you pass the string into it, rather than calling it from a property on String.

And as the other answerer mentioned, if you like, you can add it to all Strings (although I'd leverage jQuery's function rather than doing my own regexs, because of Unicode vagaries in browsers):

if (!String.prototype.trim) {
    String.prototype.trim = (function() {
        function String_trim() {
            return jQuery.trim(this);
        }
        return String_trim;
    })();
}

I've used a named function there (I always use named functions), but you could use fewer lines of code if you're not bothered:

if (!String.prototype.trim) {
    String.prototype.trim = function() {
        return jQuery.trim(this);
    };
}
like image 170
T.J. Crowder Avatar answered Jan 31 '26 09:01

T.J. Crowder