accessing radiobuttons

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martien van Wanrooij

    accessing radiobuttons

    I am working on a site with some pages that all have a form that starts with
    a group of radiobuttons. By default none of the buttons is checked. Before
    submitting the form there is a validation script that verifies if a choice
    has been made.

    The following code works, but only for one individual page
    function showChecked()
    {
    if(!document.fo rms[0].heeftPartner[0].checked &&
    !document.forms[0].heeftPartner[1].checked)alert ('Please answer this
    question');
    else document.forms[0].submit();
    }

    In every page there will come an almost similar script, only the number of
    radiobuttons belonging to the same group varies and every group also has a
    different name.
    As far as I tried out, when I write document.forms[0].elements[0] the
    "heeftPartn er" group can also be accessed, but I didn't find a method to
    acces every individual element. So I would like a script that
    - chooses the first element of the first form (which is always a radio
    button group)
    - verifies if one of the radio buttons has been checked
    If I could retrieve the number of buttons in a group by something like
    forms[0].elements[0].length I could make something like

    var theLength = forms[0].elements[0].length
    var isAnswered = false;
    for(i = 0; i < theLength; i++)
    {
    if (document.forms[0].element[0][i].checked) //and obviously this does
    not work!!!
    {
    isAnswered = true;
    break;
    }
    }
    if (isAnswered) document[0].forms[0].submit();
    else alert('Please answer this question');

    Even if the script has to be a little bit more complicated it is still worth
    the trouble because there are a lot of pages.
    Thanks for any suggestions,

    Martien van Wanrooij


  • Michael Winter

    #2
    Re: accessing radiobuttons

    On Sun, 18 Jan 2004 11:56:49 +0100, Martien van Wanrooij
    <info@martienva nwanrooij.nl> wrote:
    [color=blue]
    > I am working on a site with some pages that all have a form that starts
    > with a group of radiobuttons. By default none of the buttons is
    > checked. Before submitting the form there is a validation script that
    > verifies if a choice has been made.[/color]

    When you use form.elements[index], where form represents a form object and
    index is a positive integer, you will obtain a reference to the control at
    that index in the form. If you use form.elements['name'], where name is
    the name of the control, you will obtain a collection of all controls in
    that form, with that name.

    That's how you can access a group of radio buttons[1].

    That should help you on your way,
    Mike


    [1] There are other methods.

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • Martien van Wanrooij

      #3
      Re: accessing radiobuttons


      "Michael Winter" <M.Winter@bluey onder.co.invali d> schreef in bericht
      news:opr1yzoevy 5vklcq@news-text.blueyonder .co.uk...[color=blue]
      > If you use form.elements['name'], where name is
      > the name of the control, you will obtain a collection of all controls in
      > that form, with that name.[/color]
      Thank you, Michael, this works exactly as I wanted :)

      Martien van Wanrooij


      Comment

      • Newsgroups

        #4
        Re: accessing radiobuttons

        Here's how to reference using DHTML (vice DOM)...

        A set of radio buttons that work together all have the same name - so if
        you reference that name you get an array of all radio buttons that have
        that same name. So here is some shell code that iterates through all the
        radio buttons...

        <input type'radio' name='heeftPart ner'
        onclick='doSome thing(document. formname.heeftP artner);'
        value='whatever '>whatever</input>
        <!-- more radio buttons here all w/ the same name -->

        <script>
        function doSomething (buttonList) {
        for (var i=0; i=buttonList.le ngth; i++) {
        if (buttonList[i].selected == true) {
        // do something if this is selected
        }
        }
        }
        </script>

        Martien van Wanrooij wrote:
        [color=blue]
        > "Michael Winter" <M.Winter@bluey onder.co.invali d> schreef in bericht
        > news:opr1yzoevy 5vklcq@news-text.blueyonder .co.uk...
        >[color=green]
        >>If you use form.elements['name'], where name is
        >>the name of the control, you will obtain a collection of all controls in
        >>that form, with that name.[/color]
        >
        > Thank you, Michael, this works exactly as I wanted :)
        >
        > Martien van Wanrooij
        >
        >[/color]

        Comment

        Working...