PDA

View Full Version : Problem with changing var content from form field content ...


henning7
07-29-2004, 04:33 AM
Hi Everyone ...

I'm an old hand to programming but NEW to Javascript ... can read and modify JS but have problems creating new code at times ... I feel kinda stupid because this is really such a simple problem yet ... I can't find/see what I'm doing wrong for it not to work ...

I have a web page with a form (shopping cart form) that has 3 selection boxes ... some of the choices contain upcharges ... the form field of the "total" price ("document.shirtform.amount" = base price + upcharges) gets updated fine using various js functions and event triggers ... however, I have a displayed price below an image that I would also like to be updated along with the invisible form field ... using document.write ... I tried to "write" the form field but get an error msg (null or not an object) ... so I tried to create a separate var and function to accomplish this but ... somehow the var does NOT get updated with the content of the document.shirtform.amount field ... would someone please tell me what I'm doing wrong???

here's the code in question ...

in the <head> section ...

<script type="text/javascript">

var curr_price = "21.97"

function newCurrprice()
{
curr_price = document.shirtform.amount.value;
}
</script>
---------------------------------------------------------
in the <body> section of my page ...

<script type="text/javascript">

document.write("$"+curr_price);

</script>
----------------------------------------------------------
and the relevant parts of the form ...

<form name=shirtform target="paypal" ..........>
*
*
<input type="text" name="amount" value="21.97" onChange="newCurrprice()">
*
*
</form>

The amount field is normally "hidden" but I set it to "text" to be able to monitor the price changes ... I'll be glad to attach the complete html page if needed ...

This has got to be something VERY simple but, being very new to JS, I cannot for the life of me detect it ... Please help!!!

henning7

kwhubby
07-29-2004, 09:26 AM
well posting the whole thing would probobly help, what you have posted gives no errors but doesn't look to do what you want. Some sugestions: put your form name in quotation marks, you probobly want to document.write or whatever to be in your function newCurrprice(). Also you said you are trying to "write" to a form field, this won't work, you will need to directly define the value like formfield.value = curr_price . If you are trying to write plain text into the form element that would be something completely different.