Skip to content Skip to sidebar Skip to footer

Bootstrap Table How Hidden, When Toggled Visible Using Javascript It Does Not Show Correctly

I have a bootstrap table. I am using css to hide one of the table rows initially. I have a checkbox that I want to show the hidden table row if it is selected, otherwise it should

Solution 1:

I made a jsfiddle: https://jsfiddle.net/ov7d9rLa/

I think the problem might have been due to fighting between using bootstrap and CSS for hiding/showing. I simplified this by using the bootstrap class "hidden", and toggling that class with the click event.

<divclass="orderTotal"><h4>Order Total:</h4><tableid="tableOrderTotal"class="table tableTotal"><tbody><tr><td>Item1</td><tdclass="price-1">50</td></tr><trid='hiddenRow'class="hidden"><td>Item2</td><tdclass="price-2">13</td></tr><tr><td>Item3</td><tdclass="price-3">30</td></tr><trclass="summary"><tdclass="totalOrder">Total:</td><tdid="result"class="totalAmount"></td></tr></tbody></table></div><buttonid='toggle'class='btn btn-primary'>
Toggle
</button>

JS:

$("#toggle").on("click", function() {
    $("#hiddenRow").toggleClass("hidden");
});

Solution 2:

I wouldn't use all of that css. jQuery makes it 100x's cleaner in my opinion. Here's what I would do:

I'd take advantage of jQuery's hide() and toggle(). Something like this:

$(document).ready(function() {

  // Hide the row when the page loads
  $("#hiddenRow").hide();

  // when the user clicks the checkbox, toggle the row
  $("#toggleCheck").click(function() {

    $("#hiddenRow").toggle();

  })

});

Here's a full JSBin: http://jsbin.com/nokusunamo/edit?html,js,console,output

Solution 3:

Working fiddle.

Just use display: table-row as suggested @Louys in above comment :

#hiddenRow.show{
     display: table-row;
}

Hope this helps.

$("input[name='product_CD']").on("click", function() {
  $("#hiddenRow").toggleClass('show');
});
#hiddenRow {
  display: none;
}
.orderTotal {
  padding: 10px;
  background-color: #fdfbe4;
  width: 95%;
  margin: 0 auto;  
}
.orderTotalh4 {
  font-weight: bold;   
}
.totalOrder {
  color: #ee7a23;
  font-weight: bold;
  font-size: 18px; 
}
.totalAmount {
  font-weight: bold;
  font-size: 18px; 
}

#hiddenRow.show{
  display: table-row;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><inputname='product_CD'type="checkbox" /><divclass="orderTotal"><h4>Order Total:</h4><tableid="tableOrderTotal"class="table tableTotal"><tbody><tr><td>Item1</td><tdclass="price-1">50</td></tr><trid="hiddenRow"><td>Item2</td><tdclass="price-2">13</td></tr><tr><td>Item3</td><tdclass="price-3">30</td></tr><trclass="summary"><tdclass="totalOrder">Total:</td><tdid="result"class="totalAmount"></td></tr></tbody></table></div>

Post a Comment for "Bootstrap Table How Hidden, When Toggled Visible Using Javascript It Does Not Show Correctly"