I'm working with https://github.com/alexanderholman/Respond to try and use .addClass to add a class to a div when the browser window resizes and Bootstrap breakpoints change.
Fiddle: https://jsfiddle.net/esujd377/14/
The basic usage from github is this:
var onFunctions = {
on: {
is: {
xs: function(){console.log('is xs')},
sm: function(){console.log('is sm')},
md: function(){console.log('is md')},
lg: function(){console.log('is lg')}
},
was: {
xs: function(){console.log('was xs')},
sm: function(){console.log('was sm')},
md: function(){console.log('was md')},
lg: function(){console.log('was lg')}
}
}
};
$(document).ready(
function() {
$.respond({functions:onFunctions});
}
);
What I'm trying is this, but I get no changes in the div and no errrors in the console. What am I doing wrong?
var onFunctions = {
on: {
is: {
xs: function(){console.log('is xs')},
sm: function(){console.log('is sm')},
md: function(){ jQuery("div").addClass("whiteclass"); },
lg: function(){console.log('is lg')}
},
was: {
xs: function(){console.log('was xs')},
sm: function(){console.log('was sm')},
md: function(){ jQuery("div").addClass("whiteclass");},
lg: function(){console.log('was lg')}
}
}
};
$(document).ready(
function() {
$.respond({functions:onFunctions});
}
);
HTML:
<div class ="div">div</div>
CSS:
.div {color:red;}
.whiteclass {color:#fff;}
Your problem is how to select an element by classname in jQuery.
Change from:
To:
For more details you may take a look to Selecting by Class
The snippet is (the updated jsfiddle):