Problems with <SELECT> and hidden elements!!

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Omar

    #1

    Problems with <SELECT> and hidden elements!!

    Hi,

    In a JSP I have the next:

    ....
    codigo = "<select name='" + nombre + "'>\n<optio n selected
    value='default' >Escoge</option><option value='todos'>T odos</option>";
    if (miRS != null)
    while (miRS.next())
    {
    valorColumna = miRS.getString( nombre);
    if (valorColumna.t rim().equals("" ))
    do
    {
    miRS.next();
    valorColumna = miRS.getString( nombre);
    } while (valorColumna.t rim().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.op tions[this.selectedIn dex].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.selectedIn dex].value+

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

    TIA
  • Fred Oz

    #2
    Re: Problems with &lt;SELECT&g t; and hidden elements!!

    Omar wrote:[color=blue]
    > Hi,
    >
    > In a JSP I have the next:
    >
    > ...
    > codigo = "<select name='" + nombre + "'>\n<optio n selected
    > value='default' >Escoge</option><option value='todos'>T odos</option>";
    > if (miRS != null)
    > while (miRS.next())
    > {
    > valorColumna = miRS.getString( nombre);
    > if (valorColumna.t rim().equals("" ))
    > do
    > {
    > miRS.next();
    > valorColumna = miRS.getString( nombre);
    > } while (valorColumna.t rim().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.op tions[this.selectedIn dex].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.selectedIn dex].value+[/color]

    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.v alue = this.nombre.val ue;
    ">
    <select name="nombre">
    <option value="default" selected>Select an option</option>
    <option value="valorCol umna0">Option 0</option>
    <option value="valorCol umna1">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.valor Pos.value='defa ult';
    ">
    </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.v alue = this.nombre[this.nombre.sel ectedIndex].value;

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

    --
    Fred

    Comment

    • Omar Rodríguez

      #3
      Re: Problems with &lt;SELECT&g t; and hidden elements!!


      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 ***

      Comment

      • FredOz

        #4
        Re: Problems with &lt;SELECT&g t; and hidden elements!!

        Omar Rodríguez wrote:[color=blue]
        > 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??[/color]

        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>
        [color=blue]
        >
        > 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 ***[/color]


        --
        Fred

        Comment

        • Omar

          #5
          Re: Problems with &lt;SELECT&g t; and hidden elements!!

          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.

          Comment

          • Fred Oz

            #6
            Re: Problems with &lt;SELECT&g t; and hidden elements!!

            Omar wrote:[color=blue]
            > 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.[/color]

            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.valor Pos.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.v alue = this.nombre.val ue;
            ">
            <select name="nombre">
            <option value="default" selected>Select an option</option>
            <option value="valorCol umna0">Option 0</option>
            <option value="valorCol umna1">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.valor Pos.value='defa ult';
            ">
            <input type="submit" value="Send data...">
            </form>

            --
            Fred

            Comment

            • Omar

              #7
              Re: Problems with &lt;SELECT&g t; and hidden elements!!

              Fred, thank you for your time.
              I'll work on your idea.

              - Omar.

              Comment

              Working...