javascript - Variable returns undefined within a jquery function -
this question has answer here:
$(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 tell me why following variables returning undefined? have defined global variables jquery not seem recognize them.
this seems working expected when set html
$(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>
Comments
Post a Comment