views:

80

answers:

3

Hi,

I have the following code:

$('#smallcart .plusone').live('click',function(){
   var id = $(this).attr('id');
   articlenr = id.split('_')[1];
});

this works fine in FF, safari, Chrome, however in IE (7 and 8) it throws an error on the split-function (this property or method is not supported by this object).

if I alert the 'id'-variable I get something like plus_5751. (so I want to get the '5751' part) if I do alert(typeof(id)) I get String as an answer...

Maybe somebody can point me to the right answer?

Thx

A: 

Try renaming your variable 'id' to something else. IE doesn't like it when you name things in your scripts the same as items in the DOM.

Never mind, that seems to have not been the issue in this case. I have, however, had issues in the past with IE specific bugs caused by variable names.

Sam Dufel
That's not the problem, `id` is declared as a local variable with the `var` keyword here.
Andy E
there is not item in the dom with an `ID` of `id`
Gaby
That's a good tip: there are names that do fail (`status`, for one), but `var` should help here and `id` seems ok.
Kobi
+1  A: 

Your code works just fine for me in Internet Explorer - as it should be expected to. The problem must lie elsewhere, perhaps something is overriding String.prototype.split?. You can see a working example of your code at http://jsfiddle.net/AndyE/6K77Y/. The first thing to check for is any Internet Explorer specific code in your scripts.

I would make one small improvement for efficiency. $(this).attr('id'); is pretty much the long winded way of writing this.id. It's slower, because a new jQuery object has to be created and then the attr function has to run. Without it, your code can be compressed more, whilst still remaining very readable, if you like:

$('#smallcart .plusone').live('click',function(){
   articlenr = this.id.split('_')[1];
});
Andy E
+2  A: 

The split works pretty well in IE. The problem is the part left of the equal-sign. It's an object with all input-fields having the name articlenr and therefor IE quits with 'this property or method is not supported by this object' when you're trying to assign a string to it.

john_doe
Yes!!! It works if I put 'var' in front of 'articlenr'. Thank you so much, I hope you and everybody you love, has a wonderful day!
Ian
Your welcome ;)
john_doe
@Ian - if this answered your question, consider clicking the tick to mark it as the accepted answer.
Marc Gravell