Reputation: 45
im still new to html and learning it. right now my problem is that it wont display my input and i believe i done the code quite correctly but i could be wrong. im not sure whats wrong with it. thank you in advance!
<html>
<head>
<title>OVERSEAS VISITATION</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<form>
<table>
<tr>
<th colspan="2">
v. oversea visit
</th>
</tr>
<tr>
<td>
<b> Student activities (overseas))</b> Max RM6000 :
</td>
<td>
<input type="text" name="visit">
</td>
</tr>
</table>
<br><br>
<input type="Submit" value="Submit" onsubmit="printOutput()">
</form>
<script>
function printOutput(){
var rm = document.getElementByName('visit');
window.alert("RM :"+visit);
}
</script>
</body>
</html>
Upvotes: 0
Views: 93
Reputation: 68933
There are some issues in you code:
s
, should be getElementsByName
. And it returns collection, you have to use the proper index. Though, I prefer using querySelector()
. Also, you have to take the value
from the element.Try the following way:
<html>
<head>
<title>OVERSEAS VISITATION</title>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<form>
<table>
<tr>
<th colspan="2">
v. oversea visit
</th>
</tr>
<tr>
<td>
<b> Student activities (overseas))</b> Max RM6000 :
</td>
<td>
<input type="text" name="visit">
</td>
</tr>
</table>
<br><br>
<input type="button" value="Submit" onclick="printOutput()">
</form>
<script>
function printOutput(){
var rm = document.querySelector('[name=visit]').value;
window.alert("RM :"+rm);
}
</script>
</body>
</html>
Upvotes: 3
Reputation: 16
Change input type submit to button and in the javascript use value
Upvotes: 0
Reputation: 194
Corrected code:
<form onsubmit="printOutput()">
<table>
<tr>
<th colspan="2">
v. oversea visit
</th>
</tr>
<tr>
<td>
<b> Student activities (overseas))</b> Max RM6000 :
</td>
<td>
<input type="text" name="visit">
</td>
</tr>
</table>
<br><br>
<input type="Submit" value="Submit">
</form>
<script>
function printOutput(){
var rm = document.getElementsByName('visit');
window.alert("RM :"+ rm[0].value);
}
</script>
Three things that you had to fix in your code :
Upvotes: 1