Change value of input and submit form in JavaScript
Asked Answered
L

9

64

I'm currently working on a basic form. When you hit the submit button, it should first change the value of a field, and then submit the form as usual. It all looks a bit like this:

<form name="myform" id="myform" action="action.php">
    <input type="hidden" name="myinput" value="0" />
    <input type="text" name="message" value="" />
    <input type="submit" name="submit" onclick="DoSubmit()" />
</form>

And this is how far I've come with the JavaScript code. It changes "myinput"'s value to 1, but it does not submit the form.

function DoSubmit(){
  document.myform.myinput.value = '1';
  document.getElementById("myform").submit();
}
Lipski answered 2/8, 2011 at 12:39 Comment(3)
why not just change myinput value to 1 in the html?Skijoring
of course i could do that, but if thats what i wanted i woulden't have asked about itLipski
@Lipski That response killed it!Millpond
P
92

You could do something like this instead:

<form name="myform" action="action.php" onsubmit="DoSubmit();">
    <input type="hidden" name="myinput" value="0" />
    <input type="text" name="message" value="" />
    <input type="submit" name="submit" />
</form>

And then modify your DoSubmit function to just return true, indicating that "it's OK, now you can submit the form" to the browser:

function DoSubmit(){
  document.myform.myinput.value = '1';
  return true;
}

I'd also be wary of using onclick events on a submit button; the order of events isn't immediately obvious, and your callback won't get called if the user submits by, for example, hitting return in a textbox.

Pryce answered 2/8, 2011 at 12:44 Comment(2)
No, you can access forms and their child elements using their name attributes like this.Pryce
+1 for pointing out that onclick events are not triggered when submitting by other means than pressing the button itself.Jinx
C
7
document.getElementById("myform").submit();

This won't work as your form tag doesn't have an id.

Change it like this and it should work:

<form name="myform" id="myform" action="action.php">
Chimaera answered 2/8, 2011 at 12:42 Comment(1)
oh it has an ID, im sorry was just a little quick in my coding. edited the previous post.Lipski
D
7

Here is simple code. You must set an id for your input. Here call it 'myInput':

var myform = document.getElementById('myform');
myform.onsubmit = function(){
    document.getElementById('myInput').value = '1';
    myform.submit();
};
Dibrin answered 27/5, 2013 at 19:40 Comment(0)
H
5

No. When your input type is submit, you should have an onsubmit event declared in the markup and then do the changes you want. Meaning, have an onsubmit defined in your form tag.

Otherwise change the input type to a button and then define an onclick event for that button.

Hellenist answered 2/8, 2011 at 12:45 Comment(0)
L
2

You're trying to access an element based on the name attribute which works for postbacks to the server, but JavaScript responds to the id attribute. Add an id with the same value as name and all should work fine.

<form name="myform" id="myform" action="action.php">
  <input type="hidden" name="myinput" id="myinput" value="0" />
  <input type="text" name="message" id="message" value="" />
  <input type="submit" name="submit" id="submit" onclick="DoSubmit()" />
</form>

function DoSubmit(){
  document.getElementById("myinput").value = '1';
  return true;
}
Larrainelarrie answered 2/8, 2011 at 12:44 Comment(0)
P
1

My problem turned out to be that I was assigning as document.getElementById("myinput").Value = '1';

Notice the capital V in Value? Once I changed it to small case, i.e., value, the data started posting. Odd as it was not giving any JavaScript errors either.

Pape answered 29/3, 2014 at 23:55 Comment(4)
Why would it give an error? You're creating a new property "Value", which is perfectly fine.Vaticinal
What I am trying to say is i was referring to "value" as "Value". It of course will give a undefined error as this property "Value" was not initializedPape
Well, it would: 1) Give no error if you're setting it, 2) Silently return undefined if you're using it, which is not an error.Vaticinal
You are right..I should have clarified a bit more.. I was assigning a "Value" property and posting a form. The POST received in the the server was looking for "value" and hence never found the value I was settingPape
H
1

I have done this and it works for me. At first you must add a script such as my SetHolderParent() and call in the html code like below.

function SetHolderParent(value) {
   alert(value);
}
<input type="submit" value="Submit" onclick="SetHolderParent(222);" />
Haug answered 28/6, 2022 at 6:51 Comment(1)
Please don't provide code-only answers - it is very hard to understand stuff when it isn't explained.Elsyelton
M
0

You can use the onchange event:

<form name="myform" id="myform" action="action.php">
    <input type="hidden" name="myinput" value="0" onchange="this.form.submit()"/>
    <input type="text" name="message" value="" />
    <input type="submit" name="submit" onclick="DoSubmit()" />
</form>
Marchellemarcher answered 6/3, 2015 at 3:30 Comment(0)
C
0

This might help you.

Your HTML

<form id="myform" action="action.php">
<input type="hidden" name="myinput" value="0" />
<input type="text" name="message" value="" />
<input type="submit" name="submit" onclick="save()" />
</form>

Your Script

    <script>
        function save(){
            $('#myinput').val('1');
            $('#form').submit();
        }
    </script>
Cary answered 18/3, 2019 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.