jQuery Object doesn't support property or method trim() in IE

JqueryInternet Explorer

Jquery Problem Overview


What's up with the jQuery trim method??

jQuery('#Reminders').attr('value').trim()

Object doesn't support property or method 'trim'

jQuery('#Reminders').attr('value')

"5,1,1"

$('#Reminders').attr('value').split(',')

[5,1,1]
[0]: "5"
[1]: "1"
[2]: "1"

I don't have these woes in FireFox or Chrome ... only IE 9.0. Is there something special about trim() ... I didn't get the memo .

Jquery Solutions


Solution 1 - Jquery

IE doesn't have a string.trim() method.

Instead, you can call jQuery's $.trim(str).

Solution 2 - Jquery

You can add trim() method on String object, for browsers without support for this method (IE alike).

Simply add these lines before you call trim() method:

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

Solution 3 - Jquery

trim() is not invoked like that in a jQuery context.

Once you call attr(), that's the end of jQuery chaining. See http://api.jquery.com/attr/

To do this, do:

jQuery.trim(jQuery('#Reminders').attr('value'));

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

Solution 4 - Jquery

Same problem here with IE not having the trim() method. I solved by adding the trim() if it doesn't exist.

(function(str) {
	if (typeof(str.prototype.trim) === 'undefined') {
		str.prototype.trim = function() {
			return $.trim(this);
		};
	}
})(String);

Works great.

Solution 5 - Jquery

This doesn't have anything to do with jquery. Attr is returning a string. What it means is that IE doesn't have a trim method on string.

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
QuestionPabloView Question on Stackoverflow
Solution 1 - JquerySLaksView Answer on Stackoverflow
Solution 2 - JqueryLeonardo MontenegroView Answer on Stackoverflow
Solution 3 - JqueryartlungView Answer on Stackoverflow
Solution 4 - JqueryMauro CiancioView Answer on Stackoverflow
Solution 5 - JqueryaquinasView Answer on Stackoverflow