jQuery Form How to - Hide input file field and trigger it with code








Question

We would like to know how to hide input file field and trigger it with code.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.7.1.js'></script>
<style type='text/css'>
input {<!-- w  ww. j  a  v  a  2s  .  co m-->
  display: block;
  visibility: hidden;
  width: 0;
  height: 0;
}
</style>
<script type='text/javascript'>
$(window).load(function(){
    $('button').click(function(){
        $('input').click();
    });
});
</script>
</head>
<body>
  <input type="file" name="somename" size="chars">
  <button>Choose File</button>
</body>
</html>

The code above is rendered as follows: