Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 701
  • Last Modified:

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

0
jackie777
Asked:
jackie777
  • 6
  • 4
1 Solution
 
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
alien109Commented:
uh.. add a </html> on the end there. it got truncated for some reason.
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now