accessing selectbox value if name is variable

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

    accessing selectbox value if name is variable

    Hello!

    I have a problem accessing the value of selected option. It works fine
    if i put fixed name of select box, but not if the name is variable.
    Can anyone help me with it?



    function sel(name){
    var p = 'prefix_'+name; // name is string like id0 or id1 and so on
    var list = document.forms[0].p; //here fails the script in firefox,
    because it cannot evaluate p as name of select box
    var zonename = list.options[list.selectedIn dex].value;
    }





    Thanks!
  • Michael

    #2
    Re: accessing selectbox value if name is variable

    On Aug 22, 11:43 am, Michael <useit...@gmail .comwrote:
    Hello!
    >
    I have a problem accessing the value of selected option. It works fine
    if i put fixed name of select box, but not if the name is variable.
    Can anyone help me with it?
    >
    function sel(name){
      var p = 'prefix_'+name; // name is string like id0 or id1 and so on
      var list = document.forms[0].p; //here fails the script in firefox,
    because it cannot evaluate p as name of select box
      var zonename = list.options[list.selectedIn dex].value;
    >
    }
    >
    Thanks!
    function is called not directly in select tag, so I can't use 'this'
    in call :
    <input type="button" name="b" onclick="javasc ript:sel('id0') ;" />

    Comment

    • Michael

      #3
      Re: SOLVED: accessing selectbox value if name is variable

      On Aug 22, 11:46 am, Michael <useit...@gmail .comwrote:
      On Aug 22, 11:43 am, Michael <useit...@gmail .comwrote:
      >
      Hello!
      >
      I have a problem accessing the value of selected option. It works fine
      if i put fixed name of select box, but not if the name is variable.
      Can anyone help me with it?
      >
      function sel(name){
        var p = 'prefix_'+name; // name is string like id0 or id1 and so on
        var list = document.forms[0].p; //here fails the script in firefox,
      because it cannot evaluate p as name of select box
        var zonename = list.options[list.selectedIn dex].value;
      >
      }
      >
      Thanks!
      >
      function is called not directly in select tag, so I can't use 'this'
      in call :
      <input type="button" name="b" onclick="javasc ript:sel('id0') ;" />
      second line in function should be:
      var list = document.forms[0].elements[p];

      Comment

      • RobG

        #4
        Re: SOLVED: accessing selectbox value if name is variable

        On Aug 22, 8:23 pm, Michael <useit...@gmail .comwrote:
        [...]
        second line in function should be:
         var list = document.forms[0].elements[p];
        There is always the FAQ, though you may need to be persistent to find
        the answer:

        <URL: http://www.jibbering.com/faq/faq_not..._brackets.html >


        --
        Rob

        Comment

        • Laser Lips

          #5
          Re: SOLVED: accessing selectbox value if name is variable

          On Aug 22, 11:39 am, RobG <rg...@iinet.ne t.auwrote:
          On Aug 22, 8:23 pm, Michael <useit...@gmail .comwrote:
          [...]
          >
          second line in function should be:
          var list = document.forms[0].elements[p];
          >
          There is always the FAQ, though you may need to be persistent to find
          the answer:
          >
          <URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>
          >
          --
          Rob
          Why don't you just do ...

          var p = 'prefix_'+name;
          list = document.getEle mentById(p);

          Graham

          Comment

          • Gregor Kofler

            #6
            Re: SOLVED: accessing selectbox value if name is variable

            Laser Lips meinte:
            Why don't you just do ...
            >
            var p = 'prefix_'+name;
            list = document.getEle mentById(p);
            Why should I use gEBI() when I have got the *name* of an element?

            Gregor


            --
            http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
            http://web.gregorkofler.com ::: meine JS-Spielwiese
            http://www.image2d.com ::: Bildagentur für den alpinen Raum

            Comment

            • Gregor Kofler

              #7
              Re: SOLVED: accessing selectbox value if name is variable

              Michael meinte:
              ><input type="button" name="b" onclick="javasc ript:sel('id0') ;" />
              Forget about the "javascript :". What else than JS could be in the
              handler attributes of an element?

              Gregor


              --
              http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
              http://web.gregorkofler.com ::: meine JS-Spielwiese
              http://www.image2d.com ::: Bildagentur für den alpinen Raum

              Comment

              • RobG

                #8
                Re: SOLVED: accessing selectbox value if name is variable

                On Aug 22, 9:55 pm, Laser Lips <loudsphi...@gm ail.comwrote:
                On Aug 22, 11:39 am, RobG <rg...@iinet.ne t.auwrote:
                [...]
                There is always the FAQ, though you may need to be persistent to find
                the answer:
                >
                <URL:http://www.jibbering.c om/faq/faq_notes/square_brackets .html>
                >
                --
                Rob
                >
                Why don't you just do ...
                Why are you replying to me? And please don't quote signatures unless
                that is what you are commenting about.

                --
                Rob

                Comment

                • Thomas 'PointedEars' Lahn

                  #9
                  Re: SOLVED: accessing selectbox value if name is variable

                  Gregor Kofler wrote:
                  Michael meinte:
                  >><input type="button" name="b" onclick="javasc ript:sel('id0') ;" />
                  >
                  Forget about the "javascript :". What else than JS could be in the
                  handler attributes of an element?
                  In theory, code written in any scripting language. In practice, VBS(cript),
                  although probably only with MSHTML. For other UAs and script engines,
                  `javascript:' there this is only either a (useless) label or a syntax error.


                  PointedEars
                  --
                  Anyone who slaps a 'this page is best viewed with Browser X' label on
                  a Web page appears to be yearning for the bad old days, before the Web,
                  when you had very little chance of reading a document written on another
                  computer, another word processor, or another network. -- Tim Berners-Lee

                  Comment

                  • Gregor Kofler

                    #10
                    Re: SOLVED: accessing selectbox value if name is variable

                    Thomas 'PointedEars' Lahn meinte:
                    Gregor Kofler wrote:
                    >Michael meinte:
                    >>><input type="button" name="b" onclick="javasc ript:sel('id0') ;" />
                    >Forget about the "javascript :". What else than JS could be in the
                    >handler attributes of an element?
                    >
                    In theory, code written in any scripting language. In practice, VBS(cript),
                    although probably only with MSHTML. For other UAs and script engines,
                    `javascript:' there this is only either a (useless) label or a syntax error.
                    Right. I mixed that up with the pseudo-protocol. In this case it would
                    be just - as you stated - label (but still wrong, because that was never
                    the intention of the OP).

                    Gregor


                    --
                    http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
                    http://web.gregorkofler.com ::: meine JS-Spielwiese
                    http://www.image2d.com ::: Bildagentur für den alpinen Raum

                    Comment

                    Working...