Web Analytics Made Easy -
StatCounter help with radio buttons - CodingForum

Announcement

Collapse
No announcement yet.

help with radio buttons

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • help with radio buttons

    okay this is quite simple i'm sure, but i can't manage to get it.
    if somebody could just do stuff for something basic like this, so i can understand how on earth radio buttons are worked with:

    there are 3 radio buttons for...say...marital status. when a button is clicked, just alert the status. i tried a script with all the combinations of ifs and elses and radio button names and ids i could think of. here's one of my tries.

    <script>
    function alertstatus(){
    if (document.forms.form1.r[0].value=="yes"){
    alert('single');
    }
    if(document.forms.form1.r[1].value=="yes"){
    alert('married');
    }
    if (document.forms.form1.r[2].value=="yes"){
    alert('divorced');
    }
    }
    </script>
    <body>
    <form name="form1">
    Your marital status:
    <input type="radio" name="r" value="no"> Single
    <input type="radio" name="r" value="no"> Married
    <input type="radio" name="r" value="no"> Divorced
    </form>
    <BR>
    <input type="button" value="submit" onClick="alertstatus()">
    </body>


    WHY doesn't it work??
    when i use if-else, it always executes only the else phrase, no matter what button is checked. if i use only ifs, as above, nothing executes. i never managed to make anything work with if-else if-else. (maybe that's not the right syntax?)
    'If you don't stand for something, you'll fall for anything.'

  • #2
    asa...
    just a guessing® here 'cept tryyy n' put thisss line???
    <input type="button" value="submit" onClick="alertstatus()">

    beforrre your closing </form> tag...

    just a goodluck®...
    The New JustaBuster Version 2.0 OR JustaBusta Lite V2.0
    ...just a special® thanx kinda hugs to jkd n' nex ...:O)))

    CommemorateWTC.com --Please lend your support

    Comment


    • #3
      Hi,
      The way radio button values work is whichever one is checked, the corresponding value for that button is the value associated with the entire array. In your case, no matter what button gets checked - the value produced will always be "no". The fact that you click on them does not change their value to "yes".

      What you should be doing is determining which button was checked and then grabbing the value of that button - which should be unique. Since radio buttons all have the same name, the value is used to differentiate them. Here's an example of what I mean:

      NOTE: when I call this function I'm passing a reference to the radio array which gets referred to in the function as 'rArr'

      <script>
      function alertstatus(rArr){
      // -- loop through the array
      for (i=0;i<rArr.length;i++) {
      // -- when you arrive at the checked one
      if (rArr[i].checked) {
      // -- alert its value
      alert(rArr[i].value);
      // -- go no further
      break;
      }
      }
      }
      </script>
      <form name="form1">
      Your marital status:
      <input type="radio" name="r" value="Single"> Single
      <input type="radio" name="r" value="Married"> Married
      <input type="radio" name="r" value="Divorced"> Divorced
      <br><input type="button" value="submit" onClick="alertstatus(this.form.r)">
      </form>

      Hope that helps.
      Last edited by boywonder; Jul 7, 2002, 03:05 PM.

      Comment


      • #4
        it worked perfectly, i tried the concept on checkboxes as well, and that worked too. thanx a lot

        is it possible to change the checked button through the script, without user action? i have a form where, once the user checks something and submits, the form disappears. but when the form is invoked again, the radio button which is initially checked is the same one that the user last selected. how can i make sure the button of my choice is always the initially selected one, every time the form is invoked?
        the form is just a DIV that's set to visible when invoked and hidden when submitted (in case the info's any use).
        'If you don't stand for something, you'll fall for anything.'

        Comment

        Working...
        X