• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
Sheriffs:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
Bartenders:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Setting hidden field via javaScript

 
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.
 
Rancher
Posts: 43011
76
  • 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.
 
Ranch Hand
Posts: 341
Firefox Browser Spring Java
  • 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
 
Marshal
Posts: 67365
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
 
Whose rules are you playing by? This tiny ad doesn't respect those rules:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!