views:

36

answers:

2

i have the following html code :

 <FORM name=frmmail>
 <input id="dochtmlContent" type="hidden" name="dochtmlContent" value="oldValue"/>
 <script>document.dochtmlContent="newValue"</script>
 </FORM>

and later on in a javascript function (which is called upn submit):

     alert(document.dochtmlContent);
     document.frmmail.method = "post";
     document.frmmail.ENCTYPE = "application/x-www-form-urlencoded";
     document.frmmail.action = "/myServlet"; 
     document.frmmail.submit();

Basically, I am declaring a hiden variable, changing its value and submitting it. The issue is, while I see an alert box displaying "newValue", when I submit it, my servlet recieves the "oldValue" for the dochtmlContent parameter.

Can someone suggest whats wrong here.

+2  A: 

Change your HTML to this:

<script>document.getElementById("dochtmlContent").value = "newValue";</script>

The reason is dochtmlContent as a hidden input is not a property of document. That's not how you want to access it. Instead you are creating that property on document, but the form is still posting the hidden input, unmodified. You need to select that element using the getElementById (or another selector if relevant).

macca1
A: 
document.getElementById('dochtmlContent').value="newValue" 

worked :)

Amarsh
Normally you would just select the given answer as correct; no need to post another "answer" confirming it. :)
macca1
i had discovered the answer (hit n trial) just before i saw your answer, so thought i'll answer it myself. i saw you answer just after i submitted mine.your explaination was of great help though. thanks a lot
Amarsh