-1
 $(document).ready(function() {
  var modelNumber = false;
  var Description = false;

  $('#step-two-btn').click(function(e) {
    e.preventDefault();

    modelNumber = $("#model-number-field").val();
    Description = $("#description-field").val();

    alert(modelNumber); //undefined
    alert(Description); //undefined
  });

});

Can anyone tell me why the following variables are returning undefined? I have defined global variables but jquery does not seem to recognize them.

Brian Mains
  • 49,697
  • 35
  • 139
  • 249

2 Answers2

1

This seems to be working as expected when you set up your HTML like

 $(document).ready(function() {

  var modelNumber = false;
  var Description = false;


  $('#step-two-btn').click(function(e) {
    e.preventDefault();
    modelNumber = $("#model-number-field").val();
    Description = $("#description-field").val();
    alert(modelNumber); //undefined
    alert(Description); //undefined
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text" id="model-number-field">
<input type="text" id="description-field">
<button id="step-two-btn">click</button>
Niles Tanner
  • 3,438
  • 2
  • 13
  • 26
0

Instead of alerting the variables, try:

alert($("#model-number-field").length);
alert($("#description-field").length);

If both return 0, then it isn't finding your element by the ID. Double-check the HTML markup. Note: I'm not saying this as a permanent solution, just a quick test to do the verification.

Brian Mains
  • 49,697
  • 35
  • 139
  • 249