tonoslfx
tonoslfx

Reputation: 3442

jquery if div not id

I want to cut the price off to all input value in all divs, except only one div sale_wrap. how do I make an exception with jquery?

<div id="one_wrap">
  <input type="checkbox" name="1" value="">
  <input type="checkbox" name="1" value="">
</div>
<div id="two_wrap">
  <input type="checkbox" name="2" value="">
  <input type="checkbox" name="2" value="">
</div>
<div id="sale_wrap">
  <input type="checkbox" name="3" value="">
</div>

jquery:

if($("div").attr("id") != "sale_wrap") {
  t_balance;
} else {
  var sale = t_balance*((100-window.discount_p)/100);
  t_balance = sale;
}

Upvotes: 28

Views: 61269

Answers (5)

Om Sao
Om Sao

Reputation: 7643

Just to add little more information. One can use multiple not selector like this:

$("div:not(#sale_wrap):not(.sale_class)");

or

$("div:not(#sale_wrap, .sale_class)");

or

$("div").not('#sale_wrap, .sale_class');

Upvotes: 2

Chris Lawlor
Chris Lawlor

Reputation: 48902

The jQuery not() function can do this.

To select all div's except the div with id of sale_wrap:

$('div').not('#sale_wrap')

You can then loop over the divs with each().

As an example:

#HTML
<p id="1">Some text in paragraph 1</p>
<p id="2">Some text in paragraph 2</p>
<p id="3">Some text in paragraph 3</p>

#JS
# turn background to blue for all 'p' except p with id=2
$('p').not('#2').each(function() {
    $(this).css('background-color', 'blue');
});

Check out this example on JsFiddle.

Upvotes: 12

z33m
z33m

Reputation: 6043

Use not selector

$("div:not(#sale_wrap)")

Upvotes: 73

sh54
sh54

Reputation: 1280

Alternatively:

$('div[id != "sale_wrap"]')

Upvotes: 0

Deviprasad Das
Deviprasad Das

Reputation: 4353

Try using

if($('div').not('#sale_wrap'))
{
  t_balance;
} 
else
{
  var sale = t_balance*((100-window.discount_p)/100);
  t_balance = sale;
}

Upvotes: 3

Related Questions