EN
JavaScript - dynamically disable checkbox on click
0 points
In this article, we would like to show you how to dynamically disable checkbox element on click using JavaScript.
Quick solution:
xxxxxxxxxx
1
const myElement = document.querySelector('#my-checkbox');
2
myElement.disabled= false;
In this example, we get HTML input
element (checkbox) by id using document.querySelector()
, then we set the disabled
property inside the functions that we assign to the button
elements.
xxxxxxxxxx
1
2
<html>
3
<body>
4
<input type="checkbox" id="option" />
5
<button id="button1" onclick="disable()">disable</button>
6
<button id="button2" onclick="enable()">enable</button>
7
<script>
8
function disable() {
9
const myElement = document.querySelector('#option');
10
myElement.disabled= true;
11
}
12
13
function enable() {
14
const myElement = document.querySelector('#option');
15
myElement.disabled= false;
16
}
17
</script>
18
</body>
19
</html>