Adding an eventListener to each checkbox

Beaniie

I'm looking for a clean and concise way to add event listeners to each of my checkboxes without having to repeat my Javascript code, I'm still wet behind the ears when it comes to JS, so any assistance would be greatly appreciated.

Here's what I have so far

   function exFunction() {};

// on-click
var x = document.getElementById("checkboxopt");
x.addEventListener("click", function(){
    console.log("Opt");
  });

var y = document.getElementById("checkboxopt1");
y.addEventListener("click", function(){
    console.log("Opt 1");
  });
var z = document.getElementById("checkboxopt2");
z.addEventListener("click", function(){
    console.log("Opt 2");
  });
<div id=""><input name="checkboxopt" id="checkboxopt" value="true" type="checkbox"><br/></div>
<div id=""><input name="checkboxopt" id="checkboxopt1" value="true" type="checkbox"><br/></div>
<div id=""><input name="checkboxopt" id="checkboxopt2" value="true" type="checkbox"><br/></div>

epascarello

Either select all and loop over the collection and add the events to each element in the collection.

var cbs = document.querySelectorAll('[type="checkbox"]');
[].forEach.call(cbs, function (cb) {
    cb.addEventListener("click", function(){
        console.log(this.id);
    });
});
<div id="wrap">
  <div id="">
    <input name="checkboxopt" id="checkboxopt" value="true" type="checkbox">
  </div>
  <div id="">
    <input name="checkboxopt" id="checkboxopt1" value="true" type="checkbox">
  </div>
  <div id="">
    <input name="checkboxopt" id="checkboxopt2" value="true" type="checkbox">
  </div>
</div>

or event delegation and use the event object to figure out what element was clicked.

var wrapper = document.getElementById('wrap');
wrapper.addEventListener("click", function (evt) {
    var elem = evt.target;
    if (elem.name==="checkboxopt") {
        console.log(elem.id);
    }
});
<div id="wrap">
  <div id="">
    <input name="checkboxopt" id="checkboxopt" value="true" type="checkbox">
  </div>
  <div id="">
    <input name="checkboxopt" id="checkboxopt1" value="true" type="checkbox">
  </div>
  <div id="">
    <input name="checkboxopt" id="checkboxopt2" value="true" type="checkbox">
  </div>
</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

adding EventListener in a loop with index as argument at each iteration

From Dev

Adding eventListener for file drop

From Dev

Adding eventlistener to object in Javascript

From Dev

Adding eventlistener to javascript array

From Dev

Adding eventListener for file drop

From Dev

How to add a checkbox dynamically to each row before adding it to a table

From Dev

Adding a checkbox next to each list item using a for loop

From Dev

How to add a checkbox dynamically to each row before adding it to a table

From Dev

eventListener adding extra text elements

From Dev

Consequence of adding the same eventListener multiple times

From Dev

Javascript - undefined array element when adding eventlistener

From Dev

JavaScript adding and removing <tr> with EventListener and jQuery

From Dev

RemoveEventListener is not working when adding an EventListener inside another

From Dev

Adding a checkbox to a databound DataGridView

From Dev

Adding checkbox to a list in grails

From Dev

adding intent to checkbox

From Dev

Adding a particular checkbox at the end

From Dev

Adding checkbox to XForms repeat structure

From Dev

Adding checkbox controls to list box

From Dev

Adding an event to the checkbox in CgridView in yii

From Dev

Adding/Subtracting values from a checkbox

From Dev

Adding bindeable checkbox column to grid

From Dev

Adding checkbox controls to list box

From Dev

Adding an event to the checkbox in CgridView in yii

From Dev

Adding checkboxes to checkbox group -> listener

From Dev

Adding checkbox to IOS 6 AlertView

From Dev

Adding onclick event on dynamic checkbox

From Dev

Adding CheckBox and ProgressBar to WPF listView

From Dev

adding checkbox and id to table? localstorage

Related Related

HotTag

Archive