File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes assigning a function's result to a form field Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "assigning a function Watch "assigning a function New topic
Author

assigning a function's result to a form field

Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
Hello,

I have this js code:


so... i need to pass the selected option to a form, but it doesn;t work like i did it. While alert(elementValue) is printing the correct thing
alert('segment '+ searchForm.segment.value); print just segment. So... what is going on?

Thanks.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
You are not making sense here.

You are setting the value of a dropdown that you already knwo the text of?

Also segment here [searchForm.segment.value] is looking for a name and not an id.

Eric
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
No, i have a drop-down menu. The user selects an option and i want to get that option (the text of the option, not the value) in order to submit it to the server.
Yes, searchForm.segment.value is looking for a name and it's present. I have more fields and this works with the exception of the select element.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
well if you have one element with an id and one element with a name and they are the same value, you may be having a naming collision. Rename one of the elements.

Eric
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
Nope, checked it, no name collision.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

No, i have a drop-down menu. The user selects an option and i want to get that option (the text of the option, not the value) in order to submit it to the server.

Why are you making it hard on yourself? As it is the value that gets submitted to the server, just make that what you want submitted.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
Because i don't have a submit, but a button. I need it to pass the input params to validation functions etc. So... how do i submit what option is selected in javascript if not using the selectedIndex?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
What is searchForm.segment a hidden element, a textbox?

Eric
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
A select

Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
How are you setting the value of the select without selecting the item?

Setting the value in JavaScript will do is try to select the value. You basically would need what you do to read the text. I am not sure why you are not just setting the values when you are creating the option to begin with.

Eric
Nick Potter
Ranch Hand

Joined: May 07, 2008
Posts: 126
I'm not sure i get what you want to say. Let me try... you mean i should write something like that:
? and get it in javascript with
?

If so... what if i have different things for value and text? How do i get the text from it?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Shouldn't you be able to get that on the server?

If you want to submit the text along with it, than you need to set a hidden field with the value. That is what I thought you were doing from the start.

Or change your notation for the select value to be a way to get both sets of data. something like value="value::text" and on the server you do your magic.

Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: assigning a function's result to a form field