Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Added Inputs

I have one input with class validate and I'm appending more with same class dynamically.

$('body').on('change', $('.validade'), function() {
    var value = $(this).val();
    var IsValid = TestValue(value);
    console.log(IsValid);
});

This works event fires for all inputs added but does not bring the value. If I use:

$(".validate").change(function(){
    var value = $(this).val();
    var IsValid = TestValue(value);
    console.log(IsValid);
});

The value is ok but only works for the first input. Should I place this on the $(document).ready(function() {});? I've tried but same result.

like image 374
rgomez Avatar asked Jun 01 '26 21:06

rgomez


1 Answers

The change() will not work since the elements are dynamically generated, so you should use the event delegation .on() as you show in the first example to deal with them :

$('body').on('change', '.validade', function() {
    var value = $(this).val();
    var IsValid = TestValue(value);
    console.log(IsValid);
});

Hope this helps.

like image 97
Zakaria Acharki Avatar answered Jun 03 '26 09:06

Zakaria Acharki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!