jQuery Form How to - Display value from form inputs








Question

We would like to know how to display value from form inputs.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.10.1.js'></script>
<script type='text/javascript'>
$(window).load(function(){<!--from w  w w  . j  a v a  2 s.  co  m-->
    $('input#btn').click(function(){
        var $inputs = $('form#frm1 :input[type="text"]'),
            result = "";
        $inputs.each(function(){
            result += $(this).val()+"<br>";
        });
        $('div#result').html(result);
    });
});
</script>
</head>
<body>
  <form id="frm1">
    First name: <input type="text" name="fname" value="" /><br /> 
    Last name: <input type="text" name="lname" value="" /><br /> 
    <input type="button" id="btn" value="Submit" />
  </form>
  <div id="result">text will appear here</div>
</body>
</html>

The code above is rendered as follows: