Solved

javascript copy function

Posted on 2009-05-05
10
648 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
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

 

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 500 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to display div value on input field value in php 11 54
Google Analytics setup 5 51
Load data upon clicking a button 8 35
Button function on table is in trouble 3 22
Article by: DanRollins
This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7850-Hex-Maze-Part-2.html), we'll extend the program by adding a depth-…
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

821 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