I'm not sure there's a quick way to explain what I'm doing, so bear with me on this...
I'm setting up a series of column elements (li.element), and, upon clicking any one li.element, I want it to expand to a given percentage width and have all other column elements revert to the standard size (basically, an accordion).
The tricky part is, I have a variable number of columns to account for, and a variable width of the parent element to fill up (based on viewport width).
I've worked out the math to dynamically set both the "collapsed" and the "expanded" column widths with jQuery:
var TotalWidthInPixels = $("ul.parent").width();
var PercentageWidthOfExpandedColumn = .50
var TotalColumnCount = $("li.element").length;
var ExpandedColumnWidthInPixels = TotalWidthInPixels * PercentageWidthOfExpandedColumn
var CollapsedColumnWidthInPixels = (TotalWidthInPixels * (1.0 - PercentageWidthOfExpandedColumn)) / (TotalColumnCount - 1);
I've used the result to set widths for standard (unclassed) and collapsed (.collapsed) columns (li.element).
$("li.element").css("width", ExpandedColumnWidthInPixels);
$(".collapsed").css("width", CollapsedColumnWidthInPixels);
I also have some jQuery to add .collapsed to every li.element by default, and to remove the .collapsed from any given column when it is clicked (while also adding .collapsed to every other li.element; but I run into this error even when I don't re-add .collapsed).
//// WHEN A COLLAPSED COLUMN IS CLICKED, COLLAPSE ALL OTHERS, THEN EXPAND IT
$(".collapsed").click(function(){
$("li.element").addClass("collapsed");
$(this).removeClass("collapsed");
});
The rest of my stylesheet-set CSS (background color, etc.) works as expected with the class switches, so I can be sure that the .collapsed class is, in fact, being added and removed via click()... but the jQuery-set CSS widths are not updating. The columns remain at the value which jQuery originally established when the document was ready.
I've removed all mention of width from the stylesheet-set CSS for the elements in question, so I can be sure jQuery is the only thing driving these style widths.
Note: I had this interaction up and running as expected when the expanded and collapsed column widths were explicitly and statically set via stylesheet — even with the classes being updated via jQuery on click. The only thing that has changed is I'm now setting the widths for my classes dynamically via jQuery.
Why are my jQuery-set CSS widths not updating when jQuery updates the corresponding CSS classes?