If I have the following HTML, how can I dynamically adjust the width of all "thinger" divs?
...
<div class="thinger">...</div>
<div class="thinger">...</div>
<div class="thinger">...</div>
...
One way to do this is using jQuery like so:
$(".thinger").css("width",someWidth);
But is there a way to achieve the desired result without inline styles and instead utilize CSS classes?
...
<style>
.thinger {
width: 50px;
}
</style>
...
<div class="thinger">...</div>
<div class="thinger">...</div>
<div class="thinger">...</div>
...
I couldn't find a jQuery method dedicated to updating existing CSS classes. Does such a utility exist or should I opt for manually adding the styles using vanilla JavaScript as shown below:
var styleElement = document.createElement('style');
styleElement.type = "text/css";
styleElement.innerHTML = ".thinger {width:" + maxWidth + ";}";
document.getElementsByTagName('head')[0].appendChild(styleElement);
What are the pros and cons of each approach in terms of browser compatibility and adherence to best practices?