javascript copy function

Hi

I've got a Javascript copu function that I can't seem to get working. I am trying to copy the value of an input field into a hidden field. Any thoughts on this would be appreciated

Here is my code
<SCRIPT LANGUAGE="javascript">         
function copy()
{
 
document.order_form.SHIPPINGAMT.value = document.order_form.shipping_tot.value;
 
}
 </SCRIPT>
 
<!-- order form input fields -->
 
<input type="text" name="shipping_tot" id="shipping_tot" size="6" value="0" readonly="readonly" onfocus="this.blur()" onChange="javascript:copy()" />
   
 
<input type="hidden" name="SHIPPINGAMT" value="" />

Open in new window

jackie777Asked:
Who is Participating?
 
alien109Connect With a Mentor Commented:
uh.. add a </html> on the end there. it got truncated for some reason.
0
 
alien109Commented:
if you aren't allowing the user to update the field, how is the change event supposed to fire? as best i can tell, the event will never fire, and therefore the copy function will never get called.
0
 
jackie777Author Commented:
Thanks alien109

The field value is calculated dynamically from another function in the form that uses other input fields. The user cannot modify the input in this field. can i somehow call the function when the user clicks the submit button?
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
alien109Commented:
sure. add a submit handler function to the form. many ways to do that, but the easiest is probably:

<form onsubmit="copy()"...

that would call the copy function before submitting the form.
0
 
jackie777Author Commented:
Thanks again but still no luck.

Here is my form tag, perhaps I am doing something wrong there.

<FORM name="order_form" method="post" onsubmit="return validateFormOnSubmit(this); copy()">
0
 
alien109Commented:
you need to execute the copy function before the validation call or call the copy function within the validation function. right now, the function returns before the copy function gets executed.
0
 
alien109Commented:
<FORM name="order_form" method="post" onsubmit="copy(); return validateFormOnSubmit(this);">

or

<FORM name="order_form" method="post" onsubmit="return validateFormOnSubmit(this);">

function validateFormOnSubmit(arg) {
    copy();
    // the rest of the validation
}
0
 
jackie777Author Commented:
no still didn't work for me.

I removed the validation function, I didn't need it.

here is my code again with the changes
<SCRIPT LANGUAGE="javascript">         
function copy()
{
 
document.order_form.SHIPPINGAMT.value = document.order_form.shipping_tot.value;
 
}
</SCRIPT>
 
<FORM name="order_form" method="post" onsubmit="copy()"> 
 
<input type="text" name="shipping_tot" id="shipping_tot" size="6" value="0" readonly="readonly" onfocus="this.blur()" onChange="javascript:copy()" />
 
<input type="hidden" name="SHIPPINGAMT" value="" />

Open in new window

0
 
alien109Commented:
Then you must have something else going on...The following code produces an alert with the correct value for me.

<html>
<head>
<SCRIPT LANGUAGE="javascript">
function copy()
{

document.order_form.SHIPPINGAMT.value = document.order_form.shipping_tot.value;
alert(document.order_form.SHIPPINGAMT.value);

}
</SCRIPT>
</head>
<body>
<FORM name="order_form" method="post" onsubmit="copy()">

<input type="text" name="shipping_tot" id="shipping_tot" size="6" value="25" readonly="readonly" onfocus="this.blur()" onChange="javascript:copy()" />

<input type="hidden" name="SHIPPINGAMT" value="" />

<input type="submit" value="submit" />
</form>
</body>
0
 
jackie777Author Commented:
Thanks, i still can't get it to work, but I think it has do do with another part of my form, the code you sent works.
0
All Courses

From novice to tech pro — start learning today.