acr
acr

Reputation: 1746

javascript to hide multiple div

I have multiple div all start with expense. I am able to hide them all with below formula

$("[id^='expense']").hide();

But I have a requirement to exclude one of the div start with expense. This one will change depends upon other functions so I can not change the name.

Is there anyway I can exclude a specific one( for example expense_one)

Upvotes: 4

Views: 98

Answers (4)

Manikant Gautam
Manikant Gautam

Reputation: 3591

Here is how you can exclude elements from wildcard selector. Inside not you can pass elements separated by commas . If you want to exclude only one specific element just put only those id inside not .

$("[id^='expense']").hide()
$("[id^='expense']:not(#expense_one,#expense_two)").show();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="expense_one">1</div>
<div id="expense_two">2</div>
<div id="expense_three">3</div>
<div id="expense_four">4</div>

Upvotes: 1

sumit
sumit

Reputation: 15464

Another option is to hide all divs divs that has id starting with expense and then show the div you want on the next line. keeping it simple

$("[id^='expense']").hide();
$('#expense_one').show();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="expense_x">1</div>
<div id="expense_y">2</div>
<div id="expense_one">3</div>
<div id="expense_a">4</div>

Upvotes: 2

Anees Hikmat Abu Hmiad
Anees Hikmat Abu Hmiad

Reputation: 3560

By using not you can do this:

$("[id^='expense']").not('#expense_one').hide();

Update: id is specified using #

Upvotes: 5

Raman Kumar
Raman Kumar

Reputation: 129

Just try with below code

  $("[id^='expense']:not(:first)").hide();

Upvotes: 1

Related Questions