views:

81

answers:

1

Hi,

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  A: 

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

Jonathan Sampson
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
@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.
Jonathan Sampson