3

Given a form with the following elements.

<input name='button' value='Submit' type='submit'/>

<input name='button' value='Cancel' type='submit'/>

I want to submit the form using javascript with a completely new value.

this.form['button'] = 'MYVALUE';
this.form.submit();

How do I submit the form with a completely new value for the button parameter? If I click the button the form values contain either button=Submit OR button=Cancel I want to submit with button='MYVALUE'.

How do I achieve this.

1
  • Are you allowed to use any javascript libraries in your project? Commented Jan 16, 2010 at 18:44

1 Answer 1

1

With the following example:

<form name='myForm'>
  <input type='submit' name='subBtn' value='Submit' />
  <input type='submit' name='subBtn' value='Cancel' />
</form>

I used this:

// Intercept the 'onsubmit' function
document.myForm.onsubmit = function(){
  // Cycle through each name='subBtn'
  for (i = 0; i < this.subBtn.length; i++) {
    // Reset value property of current button in iteration
    this.subBtn[i].value = "My Value";
  }
  // Prevent form submission
  return false;
}

Demo Online: http://jsbin.com/aqenu

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

2 Comments

I need to submit the form using the new value, not intercept the submit and supply a new value. The users must also be able to click the buttons when they want to. Under some circumstances I want to submit with a new value using javascript / JQuery
@Jim: If you remove the return false; line it will submit the form with the new values. I included that line just so you can see that the values were indeed getting changed.

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.