let targetDiv = document.getElementById('hover');
targetDiv.onclick = function () {
this.style.width = '500px';
this.style.transition = 'all 1s';
this.style.backgroundColor = 'red';
}
In other words, when there are two words, background-color becomes backgroundColor.
The distinction between this and the jQuery solution:
Pure JavaScript is typically faster because it eliminates the need for additional function calls (although in most cases you wouldn't notice a difference), and it also treats it as a single element, so if the div doesn't exist, an error will be displayed in the console. This error would halt the rest of the script from executing.
On the other hand, jQuery treats elements as arrays, so if there are no matches, the array length is zero which does not trigger an error.
Instead of using getElementById
, you can utilize querySelector('#hover')
for CSS queries, however, be sure to check browser support.