• 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Read a variable "name" dynamically?

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to dynamically assign a variables name during script execution? For that matter can you do this in Java?
To be more specific, I'm reading in a lot of data from a form.
I would like to ouput a message which will be a String concatenated from the form data entered by the user. To more clearly identify that data to the end user I'd like to be able to "stick" the variable names into the String as well. I'd prefer not to hard code it but maybe I have to.
Should I just use document.formname.fieldname.name?
Thanks,
Brian Podolny
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
depends where the output string is. If it is in the same page as the form then you don't have to do much but as you yourself suggested
outputString= "some text" + document.formname.fieldname.value + "some more text"
which can be be a function which is fired by any event like onSubmit() etc...
if the info is to be passed to another page then you have to do a little more than that. Tell us exactly how you are setting this thing up if you need more help with this.
 
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have used this technique before when validating long forms. I make the name of all the form items that are required something like "reqEmail" then I do a loop through all the form items, if it starts with "req" then I check the value of the form. If it is blank then I give them a message like:
"Please enter your " + substring(3,document.form[item].name.length)"
where I just chop off the "req" portion of the name. I can't remember the exact syntax off hand, but it is something like the above.
 
Brian Podolny
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well the name property was exactly what I needed to use but thanks for the extra suggestion Bill. That nameing "trick" works nicely.
Is there a way in Java to look up a variables "name" dynamically?
( I'm asking this as an unrelated question, I'm not asking this with regards to an html page )
Thanks again.

Originally posted by bill bozeman:
I have used this technique before when validating long forms. I make the name of all the form items that are required something like "reqEmail" then I do a loop through all the form items, if it starts with "req" then I check the value of the form. If it is blank then I give them a message like:
"Please enter your " + substring(3,document.form[item].name.length)"
where I just chop off the "req" portion of the name. I can't remember the exact syntax off hand, but it is something like the above.


 
bill bozeman
Ranch Hand
Posts: 1070
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can do all sorts of things like that in Java. I am not sure how to answer your question exactly, but you can do all sorts of text manipulation, polymorphism to call various classes based on different values, and a lot more things that are not possible with Javascript or html.
 
Whip out those weird instruments of science and probe away! I think it's a tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic