0

All,

I feel like this question has been asked in many ways, and answered in just as many so I apologize. I am trying to take user input via HTML forms, and print the output back to the screen though a javaScript function.

<form id="loginInfo" >


 <input id="userName" type="text" name="user" size="40" placeholder='Username' />

 <input id="loginButton" type="image" value="submit" src="\Images\login.png" onclick="loginButtonClick()" alt="submit" height="26" />

<script>

    var username = document.forms.loginInfo.userName.value;

    function loginButtonClick() {
        alert(username);
    }
</script>

I think my issue lies in the fact that my button is an image, and I may be doing something wrong in that arena. Any help would be appreciated.

1
  • The problem is that the var username = ... runs as soon as the page loads and never again. If you move that line into the function you will get a better result. Commented Jun 6, 2014 at 20:20

1 Answer 1

1

Just put var username = document.forms.loginInfo.userName.value; inside loginButtonClick() function

So the final script will be:

<script>
    function loginButtonClick() {
        var username = document.forms.loginInfo.userName.value;
        alert(username);
    }
</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.