Syed Jafri
Syed Jafri

Reputation: 476

How to print user input from HTML form using javaScript

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.

Upvotes: 0

Views: 7981

Answers (1)

CMPS
CMPS

Reputation: 7769

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>

Upvotes: 1

Related Questions