/    Sign up×
Community /Pin to ProfileBookmark

Problem collapsing and expanding tables from clicking buttons

Hello,

It looks like you guys have a great forum here, so I decided to join and see if anybody could assist me with something that’s been driving me crazy for the past few days.

I have multiple long-width bootstrap buttons that I would like to be expandable and collapsable upon clicking; but the way I have the code now it will only keep expanding and collapsing the very first tab regardless of which button I hit.

I have a feeling I need to give each button it’s own ID and somehow call that in the bootstrap javascript, but I’ve been unable to find the solution so far.

Here is the code for 2 of the buttons, with each button having it’s own table under it that I want to collapse/expand:

First button:

[CODE]
<!– SALES AND SOURCING–>
<div class=”bs-example”>
<!– Trigger Button HTML –>
<input type=”button” class=”btn btn-block btn-info btn-lg role=”button” value=”Sales and Sourcing”>
<!– Collapsible Element HTML –>
<div id=”toggleDemo”>

<table class=”table table-bordered table-condensed table-hover”>
<!– rest of the table code –>
[/CODE]

Second button:

[CODE]
<div class=”bs-example”>
<!– Trigger Button HTML –>
<input type=”button” id=”test” class=”btn btn-block btn-info btn-lg role=”button” value=”Logistics”>
<!– Collapsible Element HTML –>
<div id=”toggleDemo1″>

<table class=”table table-bordered table-condensed table-hover”>
<!– rest of the table code –>
[/CODE]

Javascript:

[CODE]
$(document).ready(function(){
$(“.btn”).click(function(){
$(“#toggleDemo”).collapse(‘toggle’);
});
});
[/CODE]

Currently the problem is that clicking any button just opens/closes the first button’s table, but no others.

Do I need to duplicate the JS code for each button I want to collapse? How can I make it point to that instead of just all buttons?

to post a comment
JavaScript

12 Comments(s)

Copy linkTweet thisAlerts:
@JCPerezFeb 14.2014 — I recoment u tho use this jQuery code insted:

[CODE]jQuery(document).ready(function($){
$(".btn").click(function(){
$(this).next('div').collapse('toggle');
});
});[/CODE]


Try it and tell me if works....

hope to be helpful
Copy linkTweet thisAlerts:
@aj812authorFeb 18.2014 — It worked when I used that code with the regular "$(document).ready(function(){" on the first line, thank you so much!

By that same token, do you know how I could add a button that when clicked could Expand or Collapse ALL the other buttons on the page?
Copy linkTweet thisAlerts:
@JCPerezFeb 18.2014 — [CODE]jQuery(document).ready(function($){
$(".btn").click(function(){
$(this).next('div').collapse('toggle');
});

//button to expand all boxes
//.otherbtn is an example of the name of the button
$(".otherbtn").click({
if($("#toggleDemo").is(":visible")){ $("#toggleDemo").slideUp(); }
else{ $("#toggleDemo").slideDown(); }
});
})[/CODE]


And my recomendation is to chance the name of the divs ('toggleDemo', 'toggleDemo1', 'toggleDemo2', etc) to 'toggleDemo'... All them with the same name, to this code work...

hope to be helpful
Copy linkTweet thisAlerts:
@aj812authorFeb 18.2014 — Thank you JCPerez, I really appreciate your responses and they are really helping me out!

I'm having one issue with that code you just posted; it looks like when I add in the second part (for the expand all), it breaks the first part, making it so none of the buttons work? Any ideas what I could be doing wrong? Also, I just double checked and i do have all the divs id set to "toggleDemo", without any numbers

Thank you
Copy linkTweet thisAlerts:
@JCPerezFeb 19.2014 — Ok, do it me an favor... Post the full htlm with jQuery code to can help u... To see how many buttons u have and all details...

So, i can test the code and give u the solution...

And dont worry, thats why we are here, to help u to get in this amazing world ?
Copy linkTweet thisAlerts:
@JCPerezFeb 19.2014 — ohhhh sorry, i already see my mistake... this is the correct code

<i>
</i>jQuery(document).ready(function($){
$(".btn").click(function(){
$(this).next('div').collapse('toggle');
});

<i> </i>//button to expand all boxes
<i> </i>//.otherbtn is an example of the name of the button
<i> </i>$(".otherbtn").click(function(){
<i> </i> if($("#toggleDemo").is(":visible")){ $("#toggleDemo").slideUp(); }
<i> </i> else{ $("#toggleDemo").slideDown(); }
<i> </i>});
})
Copy linkTweet thisAlerts:
@aj812authorFeb 19.2014 — Thank you, I am definitely enjoying learning this, very cool to watch things start coming together

I can post the full code if I need to but it's a site with login information for websites so I'd need to remove some parts before posting it

This code now has the other buttons working normally which is a great. I made a button 'otherbtn' but the problem is that it collapses the buttons, but not the actual content "inside" the buttons that collapses out... Do you know how I could add in a part for the new button that does a collapse('toggle') on all of the other buttons so they're all collapsed or expanded?
Copy linkTweet thisAlerts:
@JCPerezFeb 19.2014 — which is ur site? so, i can see the problem?? And help u with a solution...
Copy linkTweet thisAlerts:
@aj812authorFeb 19.2014 — Here is what my code looks like with the private stuff taken out:

Thanks!

[CODE]<body>





<div class="container">
<div class="hero-unit">


</div>


<input type="button" id="otherbtn" class="btn btn-block btn-info btn-lg role="button" value="otherbtn">
<div>

<!-- FIRST CATEGORY -->
<div class="bs-example">
<!-- Trigger Button HTML -->
<input type="button" class="btn btn-block btn-info btn-lg role="button" value="Sales and Sourcing">
<!-- Collapsible Element HTML -->
<div data-role="collapsible" class="collapse" id="toggleDemo">

<table class="table table-bordered table-condensed table-hover">
<thead>

</thead>

<tbody>
<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>
<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>


</tr>
<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>
<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>
<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>

<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>r>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>

<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>

<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>
</tbody>

</table>
</div>
</div>

<br />

<!-- SECOND CATEGORY -->
<div class="bs-example">
<!-- Trigger Button HTML -->
<input type="button" class="btn btn-block btn-info btn-lg role="button" value="Logistics">
<!-- Collapsible Element HTML -->
<div data-role="collapsible" class="collapse" id="toggleDemo">

<table class="table table-bordered table-condensed table-hover">
<thead>
</thead>
<tbody>
<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

</tr>
<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><center><img></td>


</tr>

<tr>
<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>


<td align="center" style="vertical-align:middle" width="300"><center><img></td>

<td align="center" style="vertical-align:middle" width="300"><!--"><img></td>-->

</tr>
</tbody>
</table>
</div>
</div>
<br />

..........................


</body>
</html>
[/CODE]
Copy linkTweet thisAlerts:
@aj812authorFeb 20.2014 — Currently it just slides the buttons up/down, but what I want it to do is to collapse them or uncollapse them. Do you know how I could change the JS code to do this?
Copy linkTweet thisAlerts:
@aj812authorFeb 25.2014 — Hello JCPerez hope all is well, do you happen to know how to do this?
Copy linkTweet thisAlerts:
@aj812authorMar 18.2014 — Bump , does any kind person know how to tell all the buttons at once to collapse/expand?
×

Success!

Help @aj812 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...