0

I made this js script:

$(document).ready(function() {  
    if($(window).width()<768) $("#button-opl").on('click',function(){
        $("#views-exposed-form-attraktioner-block").slideToggle();  
    }); 
});

But now I want to add, if the screen is bigger then 768, then the click should be disabled. How do I do that?

thannks

2
  • Your code already does that. If the window width is less than 768, your click event won't be attached to your button.
    – Zenoo
    Commented Dec 14, 2017 at 10:55
  • I want it to update when the window is resized
    – Bel
    Commented Dec 14, 2017 at 11:44

3 Answers 3

1

You basically just need to bind an event to window if the window resizes. luckily, jQuery already does this for you. So, just go straightforward for it! This is a testeable snippet that shows you how to bind and log such event:

<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<script
  src="https://code.jquery.com/jquery-2.2.4.min.js"
  integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
  crossorigin="anonymous"></script>
  <script>      
    $( window ).resize(function() {
          $( "#log" ).append( "window was resize!!!");
        });
  </script>
<body>
    <h5 id="log"></h5>
</body>
</html>
0
1

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);
});
8
  • I would like to enable/disable the button dynamically based on when the screen size changes. How to do that? Can you give an example?
    – Bel
    Commented Dec 14, 2017 at 11:45
  • Yes, but now when I click, it opens and closes in a loop.
    – Bel
    Commented Dec 14, 2017 at 11:57
  • Apologies, I noticed the code added the on click listener every time the event fired. I have moved the on click event listener out of the resize listener. See the updated code block. Commented Dec 14, 2017 at 12:01
  • hmm, your code worked the first time, but now with the new update, the button is active, no matter screen size. The button should only be active for smartphones/tablets, and disabled when on pc.
    – Bel
    Commented Dec 14, 2017 at 12:07
  • I've amended once again to ensure the screen size is evaluated once when the page loads and then every time the screen size changes. Commented Dec 14, 2017 at 12:11
0

Since the window size can change, you should always bind the event but only act on it if the condition is met.

$(function() {
  $("#button-opl").on("click",function() {
    if( innerWidth < 768) {
      // ^ WAY faster than building a jQ object for a property that's already standard ;)
      $("#views-exposed-form-attraktioner-block").slideToggle();
    }
  });
});
2
  • I would like to enable/disable the button dynamically based on when the screen size changes. How to do that? Can you give an example?
    – Bel
    Commented Dec 14, 2017 at 11:45
  • @Bel You will need a resize event on the window, and dis-/enable the button according to the value of innerWidth. Commented Dec 14, 2017 at 13:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.