Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery Conditional

Tags:

jquery

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>
like image 454
worked Avatar asked Mar 02 '26 20:03

worked


2 Answers

You can just do this:

$('.ins.left, .ins.right').css('width','120px');
like image 116
Joseph Marikle Avatar answered Mar 07 '26 10:03

Joseph Marikle


$('.ins.left, .ins.right').css('width','120px');
like image 21
bfavaretto Avatar answered Mar 07 '26 11:03

bfavaretto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!