0

I don't know how to fix the indexof as it is bringing error ("object doesn't support this property or method".) in IE8 on win xp

var x = document.getElementById("myDIV");
var classes = x.className.split(" ");
var i = classes.indexOf("mystyle");

if (i >= 0) 
  classes.splice(i, 1);
else 
  classes.push("mystyle");
  x.className = classes.join(" "); 
  • 1
    It shouldn't produce an error. What do you actually get? – VLAZ May 05 '20 at 08:02
  • I'm sorry, i've just edited the question, the error is "object doesn't support this property or method". – yinkajewole May 05 '20 at 08:03
  • 1
    Doesn't sound like it should come from `indexOf`. – VLAZ May 05 '20 at 08:03
  • @T.J.Crowder as is WinXP... – VLAZ May 05 '20 at 08:04
  • 1
    [This](https://stackoverflow.com/questions/3629183/why-doesnt-indexof-work-on-an-array-ie8) should help. – tomerpacific May 05 '20 at 08:04
  • 1
    `indexOf` is only available from IE9 onwards. You could of course find a polyfill – phuzi May 05 '20 at 08:04
  • 2
    Side note: I strongly recommend using `{}`, and being careful with indentation. Your code is indented as though you only wrote back to `x.className` in the `else`. That isn't true (and it's correct that it isn't true), but the indentation and lack of `{}` make it misleading. – T.J. Crowder May 05 '20 at 08:04
  • @VLAZ - Same answer, XP is seriously out of date and I don't think it's receiving security patches anymore. But even on XP, use Chromium, not IE8. – T.J. Crowder May 05 '20 at 08:05
  • I just verified it with an old VM, IE8 didn't have `Array.prototype.indexOf`. Wow. – T.J. Crowder May 05 '20 at 08:07
  • I wanted to create a dropdown menu but the HTML Compiler i want to use is not supporting this `document.getElementById("myDropdown").classList.toggle("show");` So i don't know which way to go – yinkajewole May 05 '20 at 08:09
  • I suggest you can try to use the polyfill provided in this documentation link. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf?redirectlocale=en-US&redirectslug=JavaScript/Reference/Global_Objects/Array/indexOf It may help to solve your issue. The IE 8 and Win XP is too old and out of support scope of Microsoft. I suggest you try to use at least IE 11 version. – Deepak-MSFT May 05 '20 at 10:58

0 Answers0