7

I'm using the following JavaScript dropdown, which works perfect in all browers except the new Windows Edge.

It displays this error:

SCRIPT438: Object doesn't support property or method 'matches'

Script:

/* When the user clicks on the button, 
toggle between hiding and showing the dropdown content */
function myFunction() {
 document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
 if (!event.target.matches('.dropbtn')) {
 var dropdowns = document.getElementsByClassName("dropdown-content");
 var i;
 for (i = 0; i < dropdowns.length; i++) {
 var openDropdown = dropdowns[i];
 if (openDropdown.classList.contains('show')) {
 openDropdown.classList.remove('show');
 }
 }
 }
}

Got the script from: http://www.w3schools.com/howto/howto_js_dropdown.asp which I assumed would be compatible with all platforms. Now I've already implemented it, and ran into problems in Edge.

halfer
20.2k20 gold badges111 silver badges208 bronze badges
asked Apr 26, 2016 at 8:12

4 Answers 4

9

It looks like you try to check if the click event was triggered by an object with the class dropbtn.

If you use jQuery you can do the same like this:

function myFunction() {
 document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
 if (!$(event.target).hasClass('dropbtn')) {
 var dropdowns = document.getElementsByClassName("dropdown-content");
 var i;
 for (i = 0; i < dropdowns.length; i++) {
 var openDropdown = dropdowns[i];
 if (openDropdown.classList.contains('show')) {
 openDropdown.classList.remove('show');
 }
 }
 }
}

If you don't use jQuery you can get the className and then check if dropbtn is one of them.

function myFunction() {
 document.getElementById("myDropdown").classList.toggle("show");
}
// Close the dropdown menu if the user clicks outside of it
window.onclick = function(event) {
 var classes = event.target.className.split(' ');
 var found = false; var i = 0;
 while (i < classes.length && !found) {
 if (classes[i]=='dropbtn') found = true;
 else ++i;
 }
 if (!found) {
 var dropdowns = document.getElementsByClassName("dropdown-content");
 var i;
 for (i = 0; i < dropdowns.length; i++) {
 var openDropdown = dropdowns[i];
 if (openDropdown.classList.contains('show')) {
 openDropdown.classList.remove('show');
 }
 }
 }
}
answered Apr 26, 2016 at 10:17
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much @Marc Compte That was a big help :-)
5

As it was mentioned before IE11 has partial support for it. Try this

if (!Element.prototype.matches) {
 Element.prototype.matches = Element.prototype.msMatchesSelector;
}
answered Aug 16, 2018 at 12:57

Comments

3

For a cross-browser solution, look at http://youmightnotneedjquery.com/#matches_selector

var matches = function(el, selector) {
 return (el.matches || el.matchesSelector || el.msMatchesSelector || el.mozMatchesSelector || el.webkitMatchesSelector || el.oMatchesSelector).call(el, selector);
};
matches(el, '.my-class');
answered Nov 23, 2016 at 17:19

Comments

2

According to http://caniuse.com/#search=matches EDGE has partial support with the prefix 'ms'.

answered Apr 26, 2016 at 9:36

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.