0

I am using the following code to run the javascript addeventlistener. but the event is only triggerable on ID of the input field which is all different

I am trying the following way but it does nothing, no error

$('.cds').click(function() {
    var val_1 = document.getElementsByClassName('cds');
    var val_Type = $(this).attr('data-id');
    alert(val_Type);
    console.log(val_1);
    if (val_1.addEventListener) {    // For all major browsers, except IE 8 and earlier
        val_1.addEventListener("change",  function(){
            alert(val_Type);
        
    });
    } else if (val_1.attachEvent) {  // For IE 8 and earlier versions
        val_1.attachEvent("onchange",  function(){
            alert(val_Type);
            
        });
    } 
});

ids are different for each input field having the same class, so i am bit lost here how can i do it in jquery

Regual
  • 377
  • 1
  • 5
  • 20
  • Any HTML example? – ikhvjs Jun 18 '21 at 12:24
  • 1
    Does this answer your question? [What do querySelectorAll and getElementsBy\* methods return?](https://stackoverflow.com/questions/10693845/what-do-queryselectorall-and-getelementsby-methods-return) – ikiK Jun 18 '21 at 12:25
  • i have this fiddle, but its not working – Regual Jun 18 '21 at 12:27
  • https://jsfiddle.net/aL0g421w/ – Regual Jun 18 '21 at 12:31
  • 1
    Did you even read my link? `However, getElementsByClassName(), querySelectorAll(), and other getElementsBy* methods return an array-like collection of elements. Iterate over it like you would with a real array:` Here is one more: https://stackoverflow.com/questions/40956717/how-to-addeventlistener-to-multiple-elements-in-a-single-line – ikiK Jun 18 '21 at 12:32

0 Answers0