I'd like to change the value of a HTML input dynamically. The code is next:
for ( var i = 0; i < num_years; i++ ) {
$( '#Edit-ano' + i ).on( "input", function() {
var element = document.getElementById( 'Edit-ano' + i );
element.value = addCommas( $(this).val() );
});
}
But I'm receiving the next message:
Uncaught TypeError: Cannot set property 'value' of null
I've tried too passing "'Edit-ano' + i" as parameter, and using jQuery, as follow:
for ( var i = 0; i < num_years; i++ ) {
$( '#Edit-ano' + i ).on( "input", function() {
var paramater = "Edit-ano" + i;
var element = document.getElementById( paramater );
element.value = addCommas( $(this).val() );
});
}
Or:
for ( var i = 0; i < num_years; i++ ) {
$( '#Edit-ano' + i ).on( "input", function() {
var paramater = "#Edit-ano" + i;
var element = $( paramater )[0]; //also using: var element = $( "#Edit-ano" + i )[0]
element.value = addCommas( $(this).val() );
});
}
and in all the cases, I'm receiving the same error.
The code is being used in a jTable, inside the "formCreated" event.
The surprise is that if I pass the argument directly as "Edit-ano0", for example, it works perfectly! But then, I loss the dynamic behavior :(
My ideas are over, so, I hope for your answers!
Thanks in advance!