0

How does the following code look like in native JS?

$(".custom-popover").hide();

1 Answers1

2

That question is kind of broad. There is the way jQuery internally does it, and then there is a the way you can do it with just native JavaScript, independent of how jQuery may do it:

[].slice.call(
    document.querySelectorAll('.custom-popover')).forEach(function (el) {
        el.style.display = 'none';
    }
);

Since document.querySelectorAll returns a nodelist that cannot be used with forEach, you can convert it to an actual array by calling slice on the nodelist. After that, loop through everything found and update the style property.


This is an alternative that does not use forEach, although I prefer the above approach:

var els = document.querySelectorAll('.custom-popover');
for (var i = 0; i < els.length; i++) {
    els[i].style.display = 'none';
}
KevBot
  • 17,900
  • 5
  • 50
  • 68