Valentin Hristov
Valentin Hristov

Reputation: 184

How clone value input file

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>

Upvotes: 3

Views: 28129

Answers (2)

Valentin Hristov
Valentin Hristov

Reputation: 184

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>

Upvotes: 5

charlietfl
charlietfl

Reputation: 171679

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

Upvotes: 2

Related Questions