newbie newbie -4 years ago 111
jQuery Question

jQuery - using addClass on specific window width with toggleClass on same element

i have a on resize add class jQuery function which help me show an element on a certain window size:

<div class="v-menu"></div>
<div id="menu">
<a href="#">a</a>
<a href="#">b</a>
<a href="#">c</a>


(function($) {
var $window = $(window),
$html = $('#menu');
function resize() {
if ($window.width() < 612) {
return $html.addClass('close');

and i can toggle it with another function:


the problem is if i toggle the element to open and then resize the window jQuery keep adding the
class to the element:

<div id="menu" class="open close">
<a href="#">a</a>
<a href="#">b</a>
<a href="#">c</a>

how can i solve that?

Answer Source

Based on your comment

i dont want that close class add when the open class added.

 if ($window.width() < 612){
         if(!$html.hasClass('open')) {
                  return $html.addClass('close');
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download