there is an element when clicking on which the function should be executed. if the width is smaller, for example, 700рх , but the function works fine, only when the page is reloaded, and when expanding and narrowing when clicking on an element, multiple operation occurs, if I bring the condition beyond the parent function, then I write that the переменная w не определена

 function load() { h = document.documentElement.clientHeight; w = document.documentElement.clientWidth; if (w < 700) { $(".art-vmenublockheader").addClass('respunse'); } else { $(".art-vmenublockheader").removeClass('respunse'); } } $(".respunse").click(function() { alert('работает') }) $(window).resize(load); $(document).ready(load); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input type="button" class="art-vmenublockheader" value="Жми!"> 

  • It is necessary to write. Width - lower / higher, height - narrower / wider. New rules of the Russian language, adnaka - Sergey

1 answer 1

Call click - connects the event handler. It turns out that when you change the window size, you connect the same handler many times, so its execution happens several times when you click. So that this function behaves as you intend - make a width check inside the handler, and connect the handler when you only load the page.

For example:

 $(function($) { // вызывается когда готов DOM $(".art-vmenublockheader").click(function() { if ($(document).width() < 700) { //проверка ширины alert('работает'); } }); }); 
  • but I still do not understand how to make the click function run at certain window sizes. Already the class added and removed, no sense anyway it remembers the state of the element that was when the page was loaded - Evgeny Shevtsov
  • made changes, now works as it was written in previous comments - Evgeny Shevtsov
  • @ YevgenyShevtsov Completed the response code - tutankhamun
  • Everything came: checking, when clicked, should be performed, not when changing the width of the window) - Evgeny Shevtsov