Languages
[Edit]
EN

JavaScript - oninput event for input checkbox element example

9 points
Created by:
Marcin
2478

In this article few ways how to handle input event for input checkbox element has been described.

1. oninput attribute example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <label>
  	<input type="checkbox" oninput="onInput(this)" /> Agreement
  </label>
  <script>

    function onInput(element) {
    	console.log('Agreement changed to ' + element.checked + ' by oninput event.');
    }

  </script>
</body>
</html>

2. oninput property example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <label>
  	<input id="agreement" type="checkbox" /> Agreement
  </label>
  <script>

    var agreement = document.getElementById('agreement');

    agreement.oninput = function(element) {
    	console.log('Agreement changed to ' + agreement.checked + ' by oninput event.');
    };

  </script>
</body>
</html>

3. addEventListener method example

// ONLINE-RUNNER:browser;

<!doctype html>
<html>
<body>
  <label>
  	<input id="agreement" type="checkbox" /> Agreement
  </label>
  <script>

    var agreement = document.getElementById('agreement');

    agreement.addEventListener('input', function(element) {
    	console.log('Agreement changed to ' + agreement.checked + ' by input event.');
    });

  </script>
</body>
</html>

 

Hey ūüĎč
Would you like to know what we do?
  • Dirask is a friendly IT community for learners, professionals and hobbyists to share their knowledge and help each other in extraordinary easy way.
  • We welcome everyone,
    no matter what the experience,
    no matter how basic the question is,
    this community will help you.