spaces in select names

Discussion in 'Javascript' started by larry, Aug 7, 2005.

  1. larry

    larry Guest

    Hi,

    I am a newbie to Internet programming. I have some questions about
    spacing in HTML control names and subsequently being able to access
    these input elements in JavaScript If you don't have time to read the
    whole message, the main question is can HTML form names have spaces in
    them and if so what is the syntax for accessing them using Javascript

    I have a Perl script which dynamically creates html and ultimately
    generates a form. The form contains dynamically generated radio
    buttons and select controls. The value of the radio value is the same
    name as the select control name but this is only way I get the name of
    the select controls because I know exactly how many radio buttons there
    are and I know the radio button name. (if unclear disregard:not
    crucial)

    Many of the control names contain spaces. When I try to access them in
    JavaScript through the DOM it doesn't work. Can I use spaces in input
    control names.?

    I have tried to save a Javascript value(which contains spaces) in a
    variable and then use this variable in another Javascript statement.
    For example;

    selectedSG = document.Allan.SGGroup.value;
    When I print selectedSG it returns the correct value.

    But then when I try to use selectedSG in my JavaScript to access the a
    select input it doesn't work:

    document.Allan.selectedSG.length

    I have tried to hard code the select control name and it works if the
    select name has no spaces: for example

    document.Allan.Fabric.length (where fabric is a select control name)
    and then I receive the correct length for the specific select control

    However if I try to hard code a select name that contains spaces it
    doesn't work, ie.

    document.Allan.another magnificent day.length

    Clearly this syntax is wrong but I've tried bracketing and single
    quotes around another magnificent day and it still doesn't work

    Thanks ahead,
    Larry
     
    larry, Aug 7, 2005
    #1
    1. Advertising

  2. larry

    Lee Guest

    larry said:

    >Many of the control names contain spaces. When I try to access them in
    >JavaScript through the DOM it doesn't work. Can I use spaces in input
    >control names.?


    It should be trivial for you to replace spaces with underscores,
    which would make the names valid. You can access invalid names
    though, as in:

    document.Allan.elements["another magnificent day"].length
     
    Lee, Aug 7, 2005
    #2
    1. Advertising

  3. larry

    ASM Guest

    larry wrote:
    > Hi,
    >
    > I am a newbie to Internet programming. I have some questions about
    > spacing in HTML control names and subsequently being able to access
    > these input elements in JavaScript If you don't have time to read the
    > whole message, the main question is can HTML form names have spaces in
    > them and if so what is the syntax for accessing them using Javascript


    document.forms['My Own Form'].elements['My Prefered Select'];

    > The value of the radio value is the same
    > name as the select control name but this is only way I get the name of
    > the select controls because I know exactly how many radio buttons there
    > are and I know the radio button name. (if unclear disregard:not
    > crucial)


    it is not at all a good idea to give same name to radios and selects
    how can you cleanly submit your form ?
    if not correct answer : not crucial :)

    > selectedSG = document.Allan.SGGroup.value;
    > When I print selectedSG it returns the correct value.
    >
    > But then when I try to use selectedSG in my JavaScript to access the a
    > select input it doesn't work:
    >
    > document.Allan.selectedSG.length


    isn't it normal ?
    you do exactly same as writting directly the spaced name ...

    selectedSG = document.Allan.SGGroup.value;
    alert('select length = '+document.Allan.elements[selectedSG].length);

    as selectedSG is a variable, no need single quotes in brackets

    > However if I try to hard code a select name that contains spaces it
    > doesn't work, ie.
    >
    > document.Allan.another magnificent day.length


    alert('select length = ' +
    document.Allan['another magnificent day'].length);
    or
    alert('select length = ' +
    document.forms['Allan'].elements['another magnificent day'].length);
    or
    var A = document.forms['Allan'];
    alert('select length = ' +
    A.elements[A.elements['SGGroup'].value].length);

    > Clearly this syntax is wrong but I've tried bracketing and single
    > quotes around another magnificent day and it still doesn't work


    something wrong with a not necessary point ... no ?


    --
    Stephane Moriaux et son [moins] vieux Mac
     
    ASM, Aug 7, 2005
    #3
    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. John B. Matthews
    Replies:
    4
    Views:
    671
    John B. Matthews
    Sep 12, 2008
  2. Roedy Green
    Replies:
    3
    Views:
    627
  3. Tomasz Chmielewski

    [CSS] how can I show spaces as spaces?

    Tomasz Chmielewski, Sep 5, 2009, in forum: HTML
    Replies:
    21
    Views:
    1,610
    Jan C. Faerber
    Sep 10, 2009
  4. Tomasz Chmielewski

    how can I show spaces as spaces, part 2

    Tomasz Chmielewski, Sep 9, 2009, in forum: HTML
    Replies:
    14
    Views:
    879
    Jan C. Faerber
    Sep 10, 2009
  5. johkar
    Replies:
    2
    Views:
    2,904
    Mayeul
    Dec 10, 2009
Loading...

Share This Page