I am trying to make a simple javascript program bit it isn't working. Kindly help. In eclipse I have created a dynamic web project and in DD my welcome file is index.jsp. Given below is my code for index.jsp
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Duncan'S</title>
<script type="text/javascript">
function nameSubmit() {
alert(document.getElementsByName("username").value);
}
function CakeNumber() {
alert(document.getElementsByName("numOfCake").value);
}
</script>
</head>
<body>
<form action="myservlet.do">
<table>
<tr>
<td>Name:</td>
<td><input type="text" id="name" name="username" size="10"
onchange="nameSubmit();"></td>
</tr>
<tr>
<td>Number Of Duncan's Cake:</td>
<td><input type="text" id="numOfDunCake" name="numOfCake"
size="5" onchange="CakeNumber();"></td>
</tr>
</table>
</form>
</body>
</html>
In the above code both the functions are returning undefined.....!!How can I get the real value??
You have mentioned Wrong id
alert(document.getElementById("name").value);
if you want to use name
attribute then
alert(document.getElementsByName("username")[0].value);
Updates:
input type="text" id="name" name="username"
id is different from name
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With