Reputation: 5880
I simply need to apply a css style to a particular class that meets my criteria. With a simple array I'm able to do this:
var ins = document.getElementsByClassName('ins');
for(var i = 0; i < ins.length; i++) {
if(ins.item(i).attributes[0].value == "ins right" || ins.item(i).attributes[0].value == "ins left")
ins[i].style.width = "120px"; //applies the width to left and right only
}
What is the equivalent when using jQuery? (unfortunately this applies the width to all three elements)
if($('.ins').is('.left, .right'))
$('.ins').css('width','120px');
Markup:
<div id="wrapper">
<div id="outer">
<div class="ins right">
<p>This is heading 3</p>
</div>
<div class="ins left">
<p>This is heading 1</p>
</div>
<div class="ins center">
<p>This is heading 2 This is heading 2 This is heading 2 This is heading 2 This is heading 2 This is heading 2 This is heading 2 This is heading 2</p>
</div>
</div>
</div>
Upvotes: 2
Views: 860
Reputation: 95058
Your original way used a loop that went through each individual element. You need to have jquery do the same thing.
Try this, it uses implicit iteration.
$(".ins").filter(".left,.right").css('width','120px');
Upvotes: 1
Reputation: 16101
very simple, try the following:
$('.ins.right, .ins.left').css('width','120px');
Upvotes: 1
Reputation: 78590
You can just do this:
$('.ins.left, .ins.right').css('width','120px');
Upvotes: 4