To make things more efficient, it is recommended to utilize a CSS class in order to define the width of an element. This approach offers several advantages:
- No need to specify the width in multiple places (CSS and JS), making it easier to maintain
- No requirement to know the default width of the element, resulting in increased flexibility
For example:
CSS:
.wide {
width: 500px;
}
HTML:
<li onclick="show(this)">...</li>
JS:
function show(elem) {
elem.classList.toggle('wide');
}
VIEW DEMO
Take a look at the MDN documentation on classList
for details on browser support. Additionally, you can explore alternative methods for managing CSS classes on elements in this informative question/answer post: Modifying an element's class with JavaScript
To delve deeper into event handling techniques (ways to attach event handlers, data accessible within event handlers, etc.), consider reading through the insightful articles available at quirksmode.org.