Link to home
Create AccountLog in
Avatar of greddin
greddinFlag for United States of America

asked on

How can I copy one form element's value to another?

I have a page that has a primary form. In this form is a hidden field called "comments".

(See sample below)

When form1 is submitted how can I copy the "mycomments" value into the the hidden field "comments".




<textarea cols="30" rows="4" name="mycomments"></textarea>
 
<form name="form1">
<input type="hidden" name="comments">
<input type="hidden" name="id">
</form>

Open in new window

Avatar of xakem
xakem
Flag of Kazakhstan image

as an example
<html>
<head>
<script language="javascript">
function CopyValue(f)
{
	f.elements.comments.value = f.elements.id.value;
}
</script>
</head>
 
<form name="form1" onSubmit="CopyValue(this);" action="actionfile.html">
<input type="hidden" name="comments" value="commentvalue" />
<input type="hidden" name="id" value="idvalue" />
<input type="submit" value="submit" />
</form>
</html>

Open in new window

Avatar of greddin

ASKER

Well, I'm not sure I understand. Below is more of how my code appears. As you can see the textarea is not in a form. I would just like a function that gets called when "theForm" is submitted that copies the value from the "mycomments" textarea into the hidden field "comments".

Could you help with a function that does this?
<html>
<body>
 
Your comment:
<textarea cols="20" rows="4" "mycomments"></textarea>
 
<form name="theForm">
<input type="hidden" name="comments">
<input type="submit">
</form>
 
</body>
</html>

Open in new window

Sorry, my bad, just tried to show an example of how values can be copied. Can you tell me if there a possibility to copy the value on the server side or you need it on client side only?
ASKER CERTIFIED SOLUTION
Avatar of xakem
xakem
Flag of Kazakhstan image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of greddin

ASKER

Thank you xakem for going the extra step to show me.