Submit button event help.
<html>
<head>
<title>Hell WOrld</title>
</head>
<body>
<form method = "get" action = "index.html">
<input type = "text" name = "username" />
<input type = "submit" value = "submit">
</form>
<script type = "text/javascript">
document.getElementByName("username").onsubmit = alert("Hello World");
</script>
</body>
</html>
In the code, when ever you click the submit button, it says hello world, but how come the event is not triggered?
31 Minutes
Discussion Span
Related Article: Submit button on form to send to email
is a Web Development discussion thread by XxDireShadowxX that has 1 reply, was last updated 1 year ago and has been tagged with the keywords: html, submit, email.
godzab
Junior Poster in Training
65 posts since Jun 2012
Reputation Points: 0
Solved Threads: 5
Skill Endorsements: 0
The code will depend on what exactly you are trying to accomplish. If you just want to produce an alert when the button is clicked, then just add an onclick event...
<html>
<head>
<title>Hell World</title>
</head>
<body>
<form method = "get" action = "index.html">
<input type = "text" name = "username" />
<input type = "submit" onclick="alert('Hello World');" value = "submit">
</form>
</body>
</html>
JorgeM
Industrious Poster
4,024 posts since Dec 2011
Reputation Points: 297
Solved Threads: 549
Skill Endorsements: 115
THe problem is I want to do form validation:
test.html
<html>
<head>
<title>Hell WOrld</title>
</head>
<body>
<form method = "get" action = "index.html">
<input type = "text" name = "username" />
<input type = "submit" value = "submit" id = "button">
</form>
<noscript>
<h3>You need javascript</h3>
</noscript>
<script type = "text/javascript" src = "work.js">
</script>
</body>
</html>
work.js:
function handle()
{
if(this.value = "")
{
alert("You need to enter something.");
return false;
}
}
function init() {
document.getElementById("button").onclick = handle;
}
window.onload = init;
What am I doing wrong?
godzab
Junior Poster in Training
65 posts since Jun 2012
Reputation Points: 0
Solved Threads: 5
Skill Endorsements: 0
Question Answered as of 9 Months Ago by
JorgeM