• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Setting hidden field via javaScript

 
Leigh Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Anubhav Anand
Ranch Hand
Posts: 341
Firefox Browser Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rightly spotted Ulf. Seems that's the issue.

Anyway, the following are all legal and should work:

 
Leigh Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64718
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Leigh Taylor
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for that Bear and sorry bout the herring.

Leigh
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic