saranchel n
saranchel n

Reputation: 623

Jquery toggle Includes error in IE8,9,10

I am using jquery toggle code to get option for if condition. but not working.anyone find this issue. I am getting this error in IE.Object doesn't support property or method 'includes'

JS:

$("#dates").toggle(["Single", "Multi"].includes(filterType));

if (filterType === "") {

} else if (!parseInt(filterType)) { } else{}

Upvotes: 0

Views: 99

Answers (2)

Shilly
Shilly

Reputation: 8589

As written in the docs https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

Array.includes() is not supported in any version of IE. Support for it starts with MS Edge 14.

Luckily enough there's alot of polyfills you can use, or just mimic the behaviour yourself. Since you only need to know if your array includes a word, you can use .indexOf() which will return an index if the element is found, or -1 if not.

So your code becomes something like:

var toggleDates = function( filterType ) {
  var filterIsIncluded = ["Single", "Multi"].indexOf( filterType ) !== -1;
  $("#dates").toggle( filterIsIncluded );
};

$( "#setToSingle" ).click( function() {
  toggleDates( "Single" );
} );

$( "#setToOther" ).click( function() {
  toggleDates( "Other" );
} );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="dates">2018-01-11</div>
<button id="setToSingle">Set filterType to "single"</button>
<button id="setToOther">Set filterType to "other"</button>

Upvotes: 0

Pablo
Pablo

Reputation: 586

You need to add the polyfill to your code. You can find it in the documentation

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

Upvotes: 1

Related Questions