473,395 Members | 1,978 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problems with <SELECT> and hidden elements!!

Hi,

In a JSP I have the next:

....
codigo = "<select name='" + nombre + "'>\n<option selected
value='default'>Escoge</option><option value='todos'>Todos</option>";
if (miRS != null)
while (miRS.next())
{
valorColumna = miRS.getString(nombre);
if (valorColumna.trim().equals(""))
do
{
miRS.next();
valorColumna = miRS.getString(nombre);
} while (valorColumna.trim().equals(""));
if (!valorColumna.trim().equals(""))
codigo = codigo + "<option value='" + valorColumna + "'>" +
valorColumna + "</option>";
} // fin del while
codigo = codigo + "</select><input type='hidden' name='valor" +
posicion + "' value='+this.options[this.selectedIndex].value+'>";
....

There, the hidden element should get the value of the selection made
by the user in the drop down menu.

In other JSP, I have to get this value. However, the value returned
is: +this.options[this.selectedIndex].value+

How can I get the selected item value?????

TIA
Jul 23 '05 #1
6 2969
Omar wrote:
Hi,

In a JSP I have the next:

...
codigo = "<select name='" + nombre + "'>\n<option selected
value='default'>Escoge</option><option value='todos'>Todos</option>";
if (miRS != null)
while (miRS.next())
{
valorColumna = miRS.getString(nombre);
if (valorColumna.trim().equals(""))
do
{
miRS.next();
valorColumna = miRS.getString(nombre);
} while (valorColumna.trim().equals(""));
if (!valorColumna.trim().equals(""))
codigo = codigo + "<option value='" + valorColumna + "'>" +
valorColumna + "</option>";
} // fin del while
codigo = codigo + "</select><input type='hidden' name='valor" +
posicion + "' value='+this.options[this.selectedIndex].value+'>";
...

There, the hidden element should get the value of the selection made
by the user in the drop down menu.

In other JSP, I have to get this value. However, the value returned
is: +this.options[this.selectedIndex].value+


Script in a form element's value attribute won't be executed, you
should only put text in there.

If you want to put the value of the selected option into the hidden
element, the best method is to use the form's onsubmit intrinsic
event. Rather than try to munge your script, here's a simple
example:

<form action="" onsubmit="
this.valorPos.value = this.nombre.value;
">
<select name="nombre">
<option value="default" selected>Select an option</option>
<option value="valorColumna0">Option 0</option>
<option value="valorColumna1">Option 1</option>
</select><br>
<input type="hidden" name="valorPos" value="default">
<input type="button" value="Show valorPos value" onclick="
alert(this.form.valorPos.value);
">
<input type="reset" onclick="
this.form.valorPos.value='default';
">
</form>

Note that while the reset button returns the hidden element's value
to 'default' in IE, it doesn't in Firefox, hence the 'onclick' code.
This may or may not be an issue for you.

You need to decide when the value needs to be put into the the hidden
input. Using an onchange has lots of issues with making sure the
value in the hidden input is aligned with what is shown in the
select.

In addition to the reset issue above, in Firefox and IE, re-loading
the page will return the value of the hidden input to 'default', but
the selected item of 'nombre' will not change.

As a note, it seems popular to get the value of a select thusly:

this.valorPos.value = this.nombre[this.nombre.selectedIndex].value;

but I think the first mentioned method should suffice. Any comments?

--
Fred
Jul 23 '05 #2

Thank you, Fred.

As you realized, I'm newbie in this.

I have a question. Is it possible to get the value of the selected item
using JS and passing it to a JSP file??

My variable codigo (code) is used to store all the code of the HTML
page. This variable is in a Java class, which is used in a JSP file.
(Let's say JSP1)

In JSP1 there're more than 100 dropdown menus. (I mean, the code I
posted is inside a loop that repeats itself more than 100 times.) But
only one of them can be selected at the same time. However, I have to
pass all the values to the other JSP to be able to get the selected one
and to do other stuff with it.

I appreciate your help.

*** Sent via Developersdex http://www.developersdex.com ***
Jul 23 '05 #3
Omar Rodríguez wrote:
Thank you, Fred.

As you realized, I'm newbie in this.

I have a question. Is it possible to get the value of the selected item
using JS and passing it to a JSP file??
Sorry, JSP ain't my bag, baby. Someone here may volunteer, but you'd
be better off going to a JSP news group:

<URL:http://groups-beta.google.com/group/comp.lang.java.programmer>

My variable codigo (code) is used to store all the code of the HTML
page. This variable is in a Java class, which is used in a JSP file.
(Let's say JSP1)

In JSP1 there're more than 100 dropdown menus. (I mean, the code I
posted is inside a loop that repeats itself more than 100 times.) But
only one of them can be selected at the same time. However, I have to
pass all the values to the other JSP to be able to get the selected one
and to do other stuff with it.

I appreciate your help.

*** Sent via Developersdex http://www.developersdex.com ***

--
Fred
Jul 23 '05 #4
Hi,

Is it possible to set to a hidden element the value of an item
selected in a dropdown menu using a JavaScript funtion or something??

I appreciate your help.
Jul 23 '05 #5
Omar wrote:
Hi,

Is it possible to set to a hidden element the value of an item
selected in a dropdown menu using a JavaScript funtion or something??

I appreciate your help.


Yes, that was in my first post. Somehow the submit button went
missing, below is a version with a submit button.

The reason I suggested doing it on submit is because that is the only
time you can guarantee that you can make the two the same. The only
other event that makes any sense is using onchange, but I don't
recommend that for the reasons given.

Actually, some of the behaviours don't happen in version 1.03 of
Firefox - I just updated and the reset thing now works as per IE and
navigation keeps the selection and value. Re-loading the page does
not change either value. But I think the behaviour across all
browsers still warrants some extra precautions.

If you want to try it, move/copy the onsubmit code to an onchange on
the select...

<select name="nombre" onchange="
this.form.valorPos.value = this.value;
">

You could create a listener that is started by onchange and looks at
the value of the select every 50 milliseconds or so and makes the
values the same, but I can't see the point in doing it when onsubmit
will do a better job, without hogging CPU and without the 1-in-1000
chance (or maybe it's higher, I dunno) that the user changes their
selection and submits the form before the listener updates the value.

So you'd add an onsubmit anyway...
<form action="" onsubmit="
this.valorPos.value = this.nombre.value;
">
<select name="nombre">
<option value="default" selected>Select an option</option>
<option value="valorColumna0">Option 0</option>
<option value="valorColumna1">Option 1</option>
</select><br>
<input type="hidden" name="valorPos" value="default">
<input type="button" value="Show valorPos value" onclick="
alert(this.form.valorPos.value);
">
<input type="reset" onclick="
this.form.valorPos.value='default';
">
<input type="submit" value="Send data...">
</form>

--
Fred
Jul 23 '05 #6
Fred, thank you for your time.
I'll work on your idea.

- Omar.
Jul 23 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Dan R Brown | last post by:
I have a large form that is generated dynamically in a jsp using xml / xslt. So, to break up this form into several "tabbed" sections, I break up the form using <div> tags. Each <div...
1
by: Clive Moore | last post by:
I am trying to hide a drop down box in a form on a jsp page using the: style.visibility="hidden"; The form element is hidden ok but my problem is that the space still exists where the element...
1
by: Kev | last post by:
Hello, I'm pretty sure this is the right place to post and I hope someone can help me. I'm more of a cgi scripter, so I'm not 100% familiar with stylesheets but I know enough to get by. The...
0
by: aldo s. | last post by:
Hello, I suddenly cannot add new elements to any project in VS and I don't know why. There is no error message. Simply the new element is not added to the project without any message. Is there...
1
by: Gunnar | last post by:
I am finding some unusual behavior with techniques I am using to show/hide/update data without having to refresh the page. I'm quite sure it's developer ignorance on my part and would be grateful...
2
by: Noah Sussman | last post by:
Hello, I am writing a function to reposition an element based on whether one of its edges is outside the visible area of the browser window (the code is below). My client has asked for code...
3
by: libsfan01 | last post by:
Hi all whats wrong with this script? i have named a form text box dynamically using php each row has a textbox called q101, q102 ... etc and i want to make sure my users dont input a quantity...
15
by: phoenixv7 | last post by:
I need to dynamically create a number of hidden input types which have unique ids like for example name1, name2, name3 etc..... Can anyone help me???
2
by: Spizzat2 | last post by:
I've got a web page with some hidden elements that can be shown through various methods. What I'd like is, when the user tries to copy the visible stuff on the page, it doesn't copy the hidden...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.