Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How clone value input file

Tags:

jquery

file

input

How I can clone value attribute of file input field. Something like this:

<input type="file" id="field1"/>
<input type="file" id="field2"/>
<script>
$('#field2').val($('#field1').val());
</script>
like image 576
Valentin Hristov Avatar asked Nov 26 '25 10:11

Valentin Hristov


2 Answers

I found solution of this problem:

<input type="file" id="field1"/>
<span id="field2_area"><input type="file" id="field2"/></span>
<script>
$('#field1').change(function(){
    var clone = $(this).clone();
    clone.attr('id', 'field2');
    $('#field2_area').html(clone);
});
</script>
like image 55
Valentin Hristov Avatar answered Nov 28 '25 23:11

Valentin Hristov


If you are wanting them to stay the same when user interacts with them:

$(function(){
    $('#field1').on('keyup blur', function(){

            $('#field2').val($(this).val());

     }).blur();
});

Triggering the blur() on page load will do the same as code you already have

EDIT Just realized these are file fields... browser security limits what you can do with them

like image 25
charlietfl Avatar answered Nov 28 '25 22:11

charlietfl



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!