Search code examples
javascriptjqueryinputappendto

appendTo objects wont take action on jquery change


I have function on input change, it works for the first input that is already there, but when jscript adds aother
Input object with appendTo, the same function does'nt work...

$("input[type=file]").change(function() {
$('<input name="fails[]" type="file" />').appendTo('#input_lauki');
$('<a href="#" onclick="dzest(this);return false" class="link" name="dzest" style="font-weight:normal; display:inline;">Izdzēst</a><br/>').appendTo('#input_lauki')
$('.link').css('display','inline');

});

Thank you!


Solution

  • You need to use $.on to bind new elements added to the DOM as well:

    $.on("change", "input[type=file]", function() {
        $('<input name="fails[]" type="file" />').appendTo('#input_lauki');
        $('<a href="#" onclick="dzest(this);return false" class="link" name="dzest" style="font-weight:normal; display:inline;">Izdzēst</a><br/>').appendTo('#input_lauki')
        $('.link').css('display','inline');
    
    });
    

    or live if you're using an old version of jQuery, since it has some special handling that you should read about in the docs.