65

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();
}
3
  • why not just change myinput value to 1 in the html? Commented Aug 2, 2011 at 12:41
  • 57
    of course i could do that, but if thats what i wanted i woulden't have asked about it Commented Aug 2, 2011 at 12:42
  • @Paparappa That response killed it! Commented May 14, 2017 at 19:58

9 Answers 9

93

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.

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

2 Comments

No, you can access forms and their child elements using their name attributes like this.
+1 for pointing out that onclick events are not triggered when submitting by other means than pressing the button itself.
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">

1 Comment

oh it has an ID, im sorry was just a little quick in my coding. edited the previous post.
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();
};

Comments

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.

Comments

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;
}

Comments

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.

4 Comments

Why would it give an error? You're creating a new property "Value", which is perfectly fine.
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 initialized
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.
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 setting
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);" />

1 Comment

Please don't provide code-only answers - it is very hard to understand stuff when it isn't explained.
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>

Comments

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>

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.