razeal113
razeal113

Reputation: 451

changing html form value then submitting to webpage

I am simply trying to get the user name via an html button then append a string to it before submitting the concatenated string to a website. But the string doesn't seem to change

<form action="website.com" method="POST">
<input name="login" id="name" value="username" />
<script>
    var str1 = document.getElementsById("name").value;
    var str2 = " Goodbye";
    document.getElementsById("name").value = str1.concat(str2);
</script>
<button id="button" >press the button</button>

Upvotes: 0

Views: 42

Answers (2)

Jarek Kulikowski
Jarek Kulikowski

Reputation: 1389

     <form action="website.com" method="POST">
     <input name="login" id="name" value="username" />
     </form>
     <script>
         function getName() {
            var str1 = document.getElementById("name").value;
            var str2 = " Goodbye";
            document.getElementById("name").value = str1.concat(str2);
         }
        </script>
        <button id="button" onclick="getName()">press the button</button>

Upvotes: 0

Dan Kreiger
Dan Kreiger

Reputation: 5516

You are writing getElementsById, which isn't a function.

Try it singular, not plural:

document.getElementById("name")

Upvotes: 3

Related Questions