accessing selectbox value if name is variable

Discussion in 'Javascript' started by Michael, Aug 22, 2008.

  1. Michael

    Michael Guest

    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.selectedIndex].value;
    }





    Thanks!
     
    Michael, Aug 22, 2008
    #1
    1. Advertising

  2. Michael

    Michael Guest

    On Aug 22, 11:43 am, Michael <> wrote:
    > 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.selectedIndex].value;
    >
    > }
    >
    > Thanks!


    function is called not directly in select tag, so I can't use 'this'
    in call :
    <input type="button" name="b" onclick="javascript:sel('id0');" />
     
    Michael, Aug 22, 2008
    #2
    1. Advertising

  3. Michael

    Michael Guest

    Re: SOLVED: accessing selectbox value if name is variable

    On Aug 22, 11:46 am, Michael <> wrote:
    > On Aug 22, 11:43 am, Michael <> wrote:
    >
    > > 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.selectedIndex].value;

    >
    > > }

    >
    > > Thanks!

    >
    > function is called not directly in select tag, so I can't use 'this'
    > in call :
    > <input type="button" name="b" onclick="javascript:sel('id0');" />


    second line in function should be:
    var list = document.forms[0].elements[p];
     
    Michael, Aug 22, 2008
    #3
  4. Michael

    RobG Guest

    Re: SOLVED: accessing selectbox value if name is variable

    On Aug 22, 8:23 pm, Michael <> wrote:
    [...]
    > 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_notes/square_brackets.html >


    --
    Rob
     
    RobG, Aug 22, 2008
    #4
  5. Re: SOLVED: accessing selectbox value if name is variable

    Laser Lips meinte:

    > Why don't you just do ...
    >
    > var p = 'prefix_'+name;
    > list = document.getElementById(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
     
    Gregor Kofler, Aug 22, 2008
    #5
  6. Michael

    Laser Lips Guest

    Re: SOLVED: accessing selectbox value if name is variable

    On Aug 22, 11:39 am, RobG <> wrote:
    > On Aug 22, 8:23 pm, Michael <> wrote:
    > [...]
    >
    > > 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_notes/square_brackets.html>
    >
    > --
    > Rob


    Why don't you just do ...

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

    Graham
     
    Laser Lips, Aug 22, 2008
    #6
  7. Re: SOLVED: accessing selectbox value if name is variable

    Michael meinte:

    >> <input type="button" name="b" onclick="javascript: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
     
    Gregor Kofler, Aug 22, 2008
    #7
  8. Michael

    RobG Guest

    Re: SOLVED: accessing selectbox value if name is variable

    On Aug 22, 9:55 pm, Laser Lips <> wrote:
    > On Aug 22, 11:39 am, RobG <> wrote:

    [...]
    > > There is always the FAQ, though you may need to be persistent to find
    > > the answer:

    >
    > > <URL:http://www.jibbering.com/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
     
    RobG, Aug 22, 2008
    #8
  9. Re: SOLVED: accessing selectbox value if name is variable

    Gregor Kofler wrote:
    > Michael meinte:
    >>> <input type="button" name="b" onclick="javascript: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
     
    Thomas 'PointedEars' Lahn, Aug 22, 2008
    #9
  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="javascript: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
     
    Gregor Kofler, Aug 23, 2008
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthew
    Replies:
    3
    Views:
    3,797
    Natty Gur
    Aug 6, 2003
  2. Si
    Replies:
    1
    Views:
    643
  3. Bobby Chamness
    Replies:
    2
    Views:
    2,415
    Joe Smith
    Apr 22, 2007
  4. shankwheat

    Changing Value of a Selectbox

    shankwheat, Jun 22, 2006, in forum: Javascript
    Replies:
    1
    Views:
    83
    Jeremy
    Jun 22, 2006
  5. shankwheat
    Replies:
    2
    Views:
    141
Loading...

Share This Page