We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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?

2
Contributors
2
Replies
31 Minutes
Discussion Span
9 Months Ago
Last Updated
3
Views
Question
Answered
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

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0657 seconds using 2.72MB