First and foremost, I am very aware of CSS media queries. My problem is this: When you have div classes stacked in one div; Example:
JavaScript
x
2
1
<div class="class1 class2"></div>
2
And you want to remove “class2” @media (max-width: 768px) Creating an output of:
JavaScript
1
2
1
<div class="class1"></div>
2
…once the 768px threshold has been reached.
So far I have come up with nothing other than this non-functional code:
JavaScript
1
12
12
1
<script>
2
jQuery(document).resize(function () {
3
var screen = $(window)
4
if (screen.width < 768) {
5
$(".class2").hide();
6
}
7
else {
8
$(".class2").show();
9
}
10
});
11
</script>
12
I am really having a hard time finding an answer that works for this. I do not want to block the entire div’s contents! Just remove one of two classes.
Advertisement
Answer
I’m not sure I get this, but are you just trying to toggle a class?
JavaScript
1
4
1
$(window).on('resize', function () {
2
$('.class1').toggleClass('class2', $(window).width() < 768);
3
});
4
jQuery has addClass(), removeClass() and toggleClass() methods readily available.
Note that screen
is already defined in javascript.