jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes Setting hidden field via javaScript Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Setting hidden field via javaScript" Watch "Setting hidden field via javaScript" New topic
Author

Setting hidden field via javaScript

Leigh Taylor
Greenhorn

Joined: Feb 19, 2008
Posts: 20
Hi all,

I'm trying to set a hidden field within my jsp page with a value retrieved from my javascript.

I'm dealing with maps and use a javascript api which will return an 'id' based on what city was clicked. I would like to get that value from the javaScript back to the database in order to retrieve some statistical data based on that id.

From what i have read i will need to set a hidden field within my jsp, which is fine. I have set the jsp up as follows.



I would like to set this field with the javascipt. So far i've had no success using some of the following methods.



As you can see from above, i have no idea what i'm doing in regards to using javascript with the DOM. Can anybody lend some assistance here?

Thank you.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41185
    
  45
Could it have something to do with the fact that the field is named "testOuput" instead of "testOutput" (which is what the JavaScript code mentions)?

I'll move the topic to the HTML/JavaScript forum.


Ping & DNS - my free Android networking tools app
Anubhav Anand
Ranch Hand

Joined: May 18, 2007
Posts: 341

Rightly spotted Ulf. Seems that's the issue.

Anyway, the following are all legal and should work:

Leigh Taylor
Greenhorn

Joined: Feb 19, 2008
Posts: 20
Thanks for the reply guys but i only changed the field to 'testOutput' to make my problem easier to read. All names in my code do match up. Should have double checked before posting though.

On further inspection there were page errors referring to each attempt to set the 'testOutput' field (including the ones you suggested Anubhav).

All errors were similar to 'form.elements.testOutput is null or not an object'. No idea why that is? Any thoughts?

Though one of your suggestions Anubhav opened my mind a little bit. I used the getElementById to set the field.

document.getElementById("testForm:testOutput").value = "myValue";

This is the only method that worked. Any explanation would be great as i've only just ventured into the javaScript world and have no idea how to explain all this.

Thanks, Leigh
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60822
    
  65

Originally posted by Leigh Taylor:
Should have double checked before posting though.
Indeed. This is known as a "red herring" and as you can see, it only causes confusion.

All errors were similar to 'form.elements.testOutput is null or not an object'. No idea why that is? Any thoughts?
The way that you are addressing the elements is fairly archaic. The use of element ids and document.getElementId() (or equivalent in whatever JS library you might adopt), is less problematic and preferred.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Leigh Taylor
Greenhorn

Joined: Feb 19, 2008
Posts: 20
Thanks for that Bear and sorry bout the herring.

Leigh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Setting hidden field via javaScript
 
Similar Threads
Passing data between javascript and jsf
Why are getters called multiple times?
jsp not reloaded in frame
how to get the value which changed by javascript in pagecode?
Using window.location.href