• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Javascript with Struts Forms

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would like to do some changes to my page dynamically on the client side.

I have tried this as follows.

<html:select property="job" onchange="foo();">
<html ption value="auto">Allow auto selection of document.</html ption>
<html ption value="manual">Manually select a document</html ption>
</html:select>


<script lang="javascript">
function foo(){
if(document.forms[0].job == "auto"){
document.write("You want to automatically select a doc.");
}else{
document.write("You want to manually select a doc.");
}
}

What ends up happening is that document.forms[0].job is undefined! In addition to this, the document.write is outputting to a new page. What I want to happen is just have this code added into my current page. What I'm trying to achieve is if the user selects 'manual' then a new textfield will appear on the page for the user to enter an ID in.
 
Bloo Barton
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried this as follows.

<html:select property="job" onchange="foo();">
<html ption value="auto">Allow auto selection of document.</html ption>
<html ption value="manual">Manually select a document</html ption>
</html:select>

It looks like I was handling this incorrectly. Instead I believe I should access the selected value like this.

document.selectForm.batch.options[document.myForm.job.selectedIndex].value

So I could change my test to follows

if(document.selectForm.batch.options[document.myForm.job.selectedIndex].value == "auto"){
document.write("You want to automatically select a doc.");
}else{
document.write("You want to manually select a doc.");
}
}

But then when I attempted that I get a javascript error that says
'document.myForm.job.options' is null or not an object.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try referencing it like this:

document.forms[0].item('job').selectedIndex;

This will eliminate the possibility that "job" is some sort of reserved word in javascript.

Since you know the index of "auto" is zero, I'd just look for the condition: document.forms[0].item('job').selectedIndex == 0
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic