Javascript code is
function addConformationNo(){
var confirmationNo = document.getElementById("addedconfirmation").val();
alert(confirmationNo);
}
and I'm calling addConformationNo()
function on click of a button
When the addConformationNo()
function is executed, the function call to document.getElementById
returns null everytime. Why? Any clues?
Either:
DOM elements do not have val
methods associated with them, so you are likely to get a "Property is undefined or not a function" error, but that is complaining about val
not the return value of getElementById
.
Access the value
property instead.
var confirmationNo = document.getElementById("addedconfirmation").value;
var confirmationNo = document.getElementById("addedconfirmation").value;
will do.
You can also inspect element by right clicking on the browser or you can check the javascript console in your browser.
<html>
<head>
<script type=text/javascript>
function addConformationNo(){
var confirmationNo = document.getElementById("addedconfirmation").value;
alert(confirmationNo);
}
</script>
</head>
<body>
<input type = "text" name = "addedconfirmation" id = "addedconfirmation" />
<input type="submit" onclick="addConformationNo();">
</body>
</html>
If you are getting such a message for every element. Then you may have included the javascript file at the beginning of HTML on the top instead of the bottom.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.