0

So im generating few input fields and that works.

But i cant get the value of this generated input fields. I can make onchange work, but i still can't catch any value.

So i have tried a bunch of stuff with jQuery but nothing works. I alywas get undified.

 jQuery(document).on('change', function (){
        var id = "";
        alert( this.value);
}

jQuery(document).on('change', function (){
        var id = "";
        alert( jQuery(this).attr('id'));
}

This is the field that gets generated

<input class="mb-1 ml-2" style="transform: scale(1.3);" type="radio" id="input-'+counter+'">'

which will look like

<input class="mb-1 ml-2" style="transform: scale(1.3);" type="radio" id="input-1">'

That works aswell.

But i cant get the id of this input field

Driton Cazimi
  • 15
  • 1
  • 6

1 Answers1

1

this refers to the element the listener is attached to - here, that's the document. This fails with your code because the document isn't the one with the value or the id; you want the input.

Take the event argument of the change handler to get to the target, the element that the event was dispatched to:

jQuery(document).on('change', function(e) {
  console.log(e.target.value);
  console.log(e.target.id);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input class="mb-1 ml-2" style="transform: scale(1.3);" type="radio" id="input-1">'
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320