Solved

javascript copy function

Posted on 2009-05-05
10
639 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:jackie777
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 12

Expert Comment

by:alien109
Comment Utility
<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
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
uh.. add a </html> on the end there. it got truncated for some reason.
0
 

Author Closing Comment

by:jackie777
Comment Utility
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now