2

So essentially, I have an input and I want to modify the value before the user submits it. All I want to do is add a string to the end of this inputs value. I thought it would be something like:

<form action="myURL" onsubmit="myFunction" method="POST">
  <input id="myID" value="someValue" />
  <button type="submit">Press Me!</button>
</form>

<script>
myFunction()
{
    var x = document.getElementById("myID").value;
    document.getElementById("myID").value = x + "myString";
}
</script>

However, when the form is submitted the appended string is not sent and only the original string makes it. Does anyone know why this is happening?

3 Answers 3

1

You can submit the form using JavaScript:

Please Note: The function name should be preceded by the function keyword.

document.getElementById('myForm').onsubmit = myFunction;

function myFunction(){
    var x = document.getElementById("myID").value;
    document.getElementById("myID").value = x + "myString";
}
<form id="myForm" action="myURL" method="POST">
  <input id="myID" value="someValue" />
  <button type="submit">Press Me!</button>
</form>

Sign up to request clarification or add additional context in comments.

Comments

0

You can do the changes in onclick event of the submit button instead of onsubmit.

<form action="myURL" id="myForm" method="POST">
  <input id="myID" value="someValue" />
  <button type="submit" onclick="myFunction(event)">Press Me!</button>
</form>

<script>
myFunction(event)
{
    event.preventDefault(); 
    var x = document.getElementById("myID").value;
    document.getElementById("myID").value = x + "myString";
    document.getElementById("myForm").submit();
}
</script>

1 Comment

Clicking the submit button, for all it's worth, hasn't got as much to do with form submission as many people believe. For one, a form can be submitted in different ways -- a user agent like Firefox or Google Chrome, to name a few, does so when the user presses Enter on their keyboard and an input control of the form is currently focused. Your myFunction will not be called then, and you will miss the submission. Use the "submit" event that the user agent fires on form elements, instead -- that's what it's for.
0

You need to invoke the function in the submit event handler with brackets i.e. onsubmit="functionToCall()" instead of onsubmit="functionToCall".

You need the function keyword in front of the name of the function: you would write function functionName(){} instead of just functionName(){}.

You should set the variable x to equal the input element, not the input element's value so you will not need to query the DOM again.

<form action="myURL" onsubmit="myFunction()" method="POST">
  <input id="myID" value="someValue" />
  <button type="submit">Press Me!</button>
</form>
<script>
function myFunction()
{
    event.preventDefault();//for demonstrative purposes
    var x = document.getElementById("myID");
    x.value += "myString";
}
</script>

2 Comments

Why would they need to prevent submission, the question doesn't mention anything about that?
@amn Changed my answer.

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.