0

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!

3 Answers3

0

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.

Alex M
  • 3,506
  • 2
  • 20
  • 23
  • $.on needs to be applied to a selected element: the example should read `$('#input_lauki').on('change', 'input[type=file]'` – Morgan Bruce Apr 26 '12 at 08:31
0

when you defined your change event handler you had a defined amount of input file, so the new input you insert later have no handler defined for that event.

you should instead capture the event to the parent #input_lauki

$("#input_lauki").on('change', 'input[type=file]', function() {
 ...
}

example fiddle: http://jsfiddle.net/TwWEt/

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
-1

You could try

$('input[type=file]').on('change',function(){

 //your code


});
mplungjan
  • 169,008
  • 28
  • 173
  • 236
DRAKO
  • 112
  • 1
  • 2
  • 9