If you only want to evaluate the screen size when the document has loaded you could do the following:
$(document).ready(function() {
const width = $(window).width();
if (width < 768) {
// Do something
$("#button-opl").on('click', function() {
$("#views-exposed-form-attraktioner-block").slideToggle();
});
} else if (width >= 768) {
// Do something else
$("#button-opl").prop('disabled', true);
}
});
However, if you wanted to enable/disable the button dynamically based on when the screen size changes (user manually resizes the window) you would need to use an event listener and evaluate each time the window is resized like so:
$(document).ready(function() {
$("#button-opl").on('click', function() {
$("#views-exposed-form-attraktioner-block").slideToggle();
});
const reviewScreenWidth = function() {
const width = $(window).width();
if (width < 768) {
// Do something
$("#button-opl").prop('disabled', false);
} else if (width >= 768) {
// Do something else
$("#button-opl").prop('disabled', true);
}
}
// Call once when the page has loaded
reviewScreenWidth();
// Call every time the screen size changes
window.addEventListener("resize", reviewScreenWidth);
});