Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

javascript copy function

Posted on 2009-05-05
10
Medium Priority
?
675 Views
Last Modified: 2012-05-06
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
Comment
Question by:jackie777
  • 6
  • 4
10 Comments
 
LVL 12

Expert Comment

by:alien109
ID: 24309895
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
 

Author Comment

by:jackie777
ID: 24309953
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
 
LVL 12

Expert Comment

by:alien109
ID: 24310158
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:jackie777
ID: 24310234
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
 
LVL 12

Expert Comment

by:alien109
ID: 24310300
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
 
LVL 12

Expert Comment

by:alien109
ID: 24310312
<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
 

Author Comment

by:jackie777
ID: 24310388
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
 
LVL 12

Expert Comment

by:alien109
ID: 24310768
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
 
LVL 12

Accepted Solution

by:
alien109 earned 2000 total points
ID: 24310773
uh.. add a </html> on the end there. it got truncated for some reason.
0
 

Author Closing Comment

by:jackie777
ID: 31578270
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

782 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question