4

when i test my web-page in chrome and firefox they work fine. but in IE it does not worked.

i found that

(" .class li").text().trim() not worked in IE he give me error that

Object doesn't support this property or method. but in FF and chrome they work fine. are i goes something wrong to handle this.

James Black
  • 41,583
  • 10
  • 86
  • 166

3 Answers3

15

Try this:

$.trim($(".class li").text());

The reason that it doesn't worked in your case is because the trim method you were calling was wasn't jquery.trim method. It is a method you were calling on a object instance (.text() returns a string). So some browsers have this method built-in while IE doesn't.

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Well then you will have to do the same everywhere in your code. Look at the jquery documentation of the $.trim function and you will see that it is used exactly like that. If you are still getting error messages it is not on this function. – Darin Dimitrov Nov 20 '10 at 10:25
1

If you want to remove both leading and trailing spaces you need to use

replace(/^\s+|\s+$/g, '')
manish nautiyal
  • 2,556
  • 3
  • 27
  • 34
1

String.trim is not part of the old language specification, it is a new kid in town. Fortunetely you can easily add this function.

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

Now you can trim any String you want:

" just do it   ".trim()
$(" .class li").text().trim()
gblazex
  • 49,155
  • 12
  • 98
  • 91