4

While debugging some jQuery that is not working in IE, I found this error message:

var item = $("#item_"+ itemArray[itemIndex]).find('a').text().trim();

Object doesn't support this property or method (script.js, line 100, character 2)

The character 2 doesn't make sense to me. Based on the text displayed character 2 would be the letter a in var but of course that doesn't make any sense.

(Should I not use var?)

I know that jQuery is working to some extent or the script would not have been able to get this far on my page.

Roger Barr
  • 41
  • 2
  • 2
    Why? Because it is Internet Explorer. –  Nov 30 '10 at 15:49
  • 2
    possible duplicate of [.trim() in JavaScript not working in IE](http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie) – Stephen Nov 30 '10 at 15:52
  • @Stephen - That's a non-jQuery question, the optimal answer differs. – Nick Craver Nov 30 '10 at 16:02
  • This has been asked before: http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie Basically, it looks like IE does not have `trim()` built-in. – Orbling Nov 30 '10 at 15:50

2 Answers2

19

IE doesn't have String.trim(), you'll need $.trim() (which uses native trim if available, emulates it in IE), like this:

var item = $.trim($("#item_"+ itemArray[itemIndex]).find('a').text());
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Be quick, Roger! Accept this as this question gets closed soon :') Nick's 115k isn't yet enough! –  Nov 30 '10 at 15:59
  • @Time - just to note it shouldn't be closed with *that* duplicate, there's a much easier work-around here than a non-jquery question (which is marked as the duplicate), which I've provided above :) – Nick Craver Nov 30 '10 at 16:01
  • Aha, I see, it's not jQuery-related. (Y) –  Nov 30 '10 at 16:03
  • Potato Patata. Well, not really. Okay, you win. ;) – Stephen Nov 30 '10 at 16:07
3

IE doesn't have a trim method.

Instead, you can call jQuery.trim(...).

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964