Basic Form processing

Basic Form processing

i got this little problem in understanding the code. i get null value of the form component with the "not-working" code, but it does get the value on the "working-code". anyone can explain? thanks

here's my form
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="js/jquery.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/form.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post">
  Nama
  <input type="text" name="name" id="name" />
  <br />
  Password
  <input type="text" name="pwd" id="pwd" />
  <input type="submit" name="button" id="button" value="Submit" />
</form>
</body>
</html>


here's not working code
$(function(){
   var button=$("#button");
   var name=$("#name").val(),
   pwd=$("#pwd").val();
   
   button.click(function(){
   alert(name+pwd);   
   return false;
});
});


and here's the working code
$(function(){
   var button=$("#button");
   
   button.click(function(){
            var name=$("#name").val(),
   pwd=$("#pwd").val();
   alert(name+pwd);   
   return false;
});
});