javascript - Change handler not working -
i new jquery , having trouble getting simple html page containing jquery (that gleaned stack overflow thread, unfortunately wasn't complete enough jquery newbie me, apparently!) work properly. when choose filename, expect "mytext" text input field contain name of file uploaded, not happen.
$(document).ready(function() { $('#myfile').bind('change', function() { var filename = ''; filename = $(this).val(); $('#mytext').html(filename); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <form> <input type="text" id="mytext" name="mytext"> <input type="file" id="myfile" name="myfile"> </form>
- you missing /script on jquery tag
- you using .html() instead of .val() field
- you should use
.on
instead of.bind
$('#myfile').on('change', function() { filename = $(this).val().split('\\').pop(); $('#mytext').val(filename); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="text" id="mytext" name="mytext"> <input type="file" id="myfile" name="myfile">
Comments
Post a Comment