I’m using this bit of javascript to add a class to multiple elements. I trying to reference multiple divs and addClass to them. it Only works on the first one.
Javascript
<script>
$(function(){
if ( $(window).width() < 230 ) {
$('#item1').addClass('col1'); //max-width 80px
}
else
if ( $(window).width() >= 230 && $(window).width() < 330 ) {
$('#item1').addClass('col2'); //max-width 180px
}
else
{
$('#item1').addClass('col3'); //max-width 280px
}
});
</script>
it’s working if I only have one #item1 on the page but it doesn’t seem to want to add the class to multiple items that exist in the same div id. I even tried adding the class to multiple items that exist in the same div class rather than id. Either one would be a good solution for mw if you can figure it out.
My HTML code is
<div id="item1" class="blue">
<label for="amount">Price range:</label>
<input type="text" id="amount" style="border:0; color:#f6931f; font-weight:bold;" />
<div class="slider-range"></div>
</div>
<div id="item1" class="blue">
<label for="amount2">Price range:</label>
<input type="text" id="amount2" style="border:0; color:#f6931f; font-weight:bold;"/>
<div class="slider-range2"></div>
</div>
Been trying to figure this out for a few hours now so I thought I’d ask to see if anyone can point me in the right direction. Thanks in advance.
IDs should be unique. Try adding a class instead, like “item”:
Then in your JS: