admin管理员组文章数量:1405880
I'm unable to access the variable in the JS file.
The Validation is working fine when written in the same page. But when I pasted the Validation code to a separate JS file, its not working.
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script src="Scripts/NewValidation.js" type="text/javascript"></script>
<table id="tblUpdateReg">
<thead>
<tr>
<td>
Update Details
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
Username :
</div>
</td>
<td>
<input type="text" runat="server" id="txtUsername" />
</td>
</tr>
</tbody>
</table>
</asp:Content>
JS file:
function Validate() {
var username= document.getElementById("txtUsername").value;
if (username== "") {
alert("username Required.");
return false;
}
return true;
}
Error:JavaScript runtime error: Unable to get property 'value' of undefined or null reference
I'm unable to access the variable in the JS file.
The Validation is working fine when written in the same page. But when I pasted the Validation code to a separate JS file, its not working.
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<script src="Scripts/NewValidation.js" type="text/javascript"></script>
<table id="tblUpdateReg">
<thead>
<tr>
<td>
Update Details
</td>
</tr>
</thead>
<tbody>
<tr>
<td>
<div>
Username :
</div>
</td>
<td>
<input type="text" runat="server" id="txtUsername" />
</td>
</tr>
</tbody>
</table>
</asp:Content>
JS file:
function Validate() {
var username= document.getElementById("txtUsername").value;
if (username== "") {
alert("username Required.");
return false;
}
return true;
}
Error:JavaScript runtime error: Unable to get property 'value' of undefined or null reference
Share Improve this question asked Aug 29, 2013 at 6:36 PearlPearl 9,4558 gold badges45 silver badges60 bronze badges 1- 1 Are you calling validate function after the above content has been loaded ? – Voonic Commented Aug 29, 2013 at 6:38
1 Answer
Reset to default 1It means that the element with an id of txtUsername doesn't exist. You execute your script before the element is created.
You should either put your script at the bottom of the page or use a
window.onload = function() {}
in which you'll execute your code.
本文标签:
版权声明:本文标题:jquery - JavaScript runtime error Unable to get property 'value' of undefined or null reference - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744945965a2633780.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论