4

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

5
  • 1
    possible duplicate of stackoverflow.com/questions/1453521/… Commented Oct 19, 2010 at 8:12
  • @Paniyar: that's not a duplicate. The issue in that question is due to the use of regular expressions to split a string - this question is about splitting a string by another string. Commented Oct 19, 2010 at 8:14
  • I cannot reproduce it: jsbin.com/imoki3 Commented Oct 19, 2010 at 8:15
  • works fine for me ( example ). How about adding the script tag, and better yet a link to a working page.. Commented Oct 19, 2010 at 8:15
  • @Kobi - really? I knew it! IE is getting personal with me! Still throwing an error when I try it... :-( Commented Oct 19, 2010 at 9:15

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 - if this answered your question, consider clicking the tick to mark it as the accepted answer.
2

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];
});

Comments

0

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.

3 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.