Alternating attributes without values
Let a disabled input and a button be given:
<input id="elem" disabled>
<input id="button" type="submit">
Let's make it so that each click on the button causes the input to change its state - from disabled to enabled and vice versa. First, let's get references to our elements into variables:
let elem = document.querySelector('#elem');
let button = document.querySelector('#button');
Now let's implement what we've planned:
button.addEventListener('click', function() {
if (elem.disabled) {
elem.disabled = false;
} else {
elem.disabled = true;
}
});
The problem, however, can be solved with less code:
button.addEventListener('click', function() {
elem.disabled = !elem.disabled;
});
Explain how the above code works.
Given a checkbox and a button. Make sure that each button click causes the checkbox to change its state.