IE will not split the string using Javascript

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 for the split function (this property or method is not supported by this object).

if I warn '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 the response ...

Can someone please point me to the correct answer?

thanks

+4
source share
3 answers

split works very well in IE. The problem is the left side of the equal sign. This is an object with all input fields named articlenr , and therefore IE terminates with "this property or method is not supported by this object" when you try to assign a string to it.

+4
source

Your code works fine for me in Internet Explorer - as you would expect. The problem should be somewhere else, maybe 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 is any specific Internet Explorer code in your scripts.

I would make a slight performance improvement. $(this).attr('id'); is a pretty long way to write this.id This is slower because you need to create a new jQuery object and then run the attr function. Without this, your code can be compressed more, while remaining very readable if you want:

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

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

Nothing, this did not seem to be a problem in this case. However, I had problems with IE with errors caused by variable names.

0
source

All Articles