Struggling with JQuery to toggle open/close three table rows in a asp.net web form page, I have managed to use the scripts provided below. However, whenever I click on the "Submit" button causing a postback, the opened table rows close and the page reverts to its initial state. Is there a way to preserve the open table row state after the postback?
(this code utilizes jquery-2.2.4.js and font-awesome 4.7.0)
- JQuery script:
<script type="text/javascript">
$(function () {
var $table = $("table");
$table.find(".activator").click(function () {
$(this).toggleClass("open");
});
});
</script>
CSS class:
.filterTable {margin: auto; border: 0px solid #808080;} .activator {cursor: pointer;} .activator.open, .activator:hover {background: #f8f3ba; border:1px dashed #808080} .activator.open .fa-plus-square-o, .activator .fa-minus-square-o, .activator + .hidden-row {display: none;} .activator.open + .hidden-row {display: table-row;} .activator.open .fa-minus-square-o {display: inline-block;}
HTML code:
<div> <table class="filterTable"> <tbody> <tr class="activator"> <td><i class="fa fa-plus-square-o"></i><i class="fa fa-minus-square-o"></i> Row 1</td> </tr> <tr class="hidden-row"> <td>Row 1 content<br /> - row 1</td> </tr> <tr class="activator"> <td><i class="fa fa-plus-square-o"></i><i class="fa fa-minus-square-o"></i> Row 2</td> </tr> <tr class="hidden-row"> <td>Row 2 content<br /> - row 2</td> </tr> <tr class="activator"> <td><i class="fa fa-plus-square-o"></i><i class="fa fa-minus-square-o"></i> Row 3</td> </tr> <tr class="hidden-row"> <td>Row 3 content<br /> - row 3</td> </tr> </tbody> </table> <div style="height:30px;"></div> <asp:Button ID="Button1" Text="Submit" runat="server" CssClass="btn btn-primary" /> </div>
Image a - initial page:
image of the initial state of page
Image b - toggled opened (when postback this state cannot maintain)
image of the table-row expanded state of page, which will not maintain after postback