This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Populating JSP text field with Javascript?

 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have been trying to solve this for sometime now and it does not seem to work.

I have a text field defined in a jsp page:



I have this fancy Javascript function which searches a list and returns the result. Search is working fine. The result needs to show up in the above text field. The code below is supposed to make that happen:



searchlist.options[0].text contains the results of my search - I have verified this. classText.value does not seem to take the search result.

Help please?
 
Michael Ku
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think that you need to have an id attribute not property attribute in your html:text tag. property is related to your Struts ActionForm property.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assign an id to the field and use document.getElementById() to access it. This is the most staright-forward and accepted means to reference DOM elements.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64824
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Populating JSP text field
A point of clarification: there is no such thing as a "JSP text field". Regardless of how you specify it in a JSP, by the time it gets to the browser, it's just an HTML element like any other. To see what the Struts tag generated, do a View Source in the browser. This is what your JavaScript will be dealing with.
 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the suggestions. I'll give it a try and post results.
 
Chris Stann
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, well, well... the code works.. In Internet Exploder! Argh... I was testing in Firefox.

Note to self: Always test using multiple browsers without assumptions of cross-compatibility.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic