Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy 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?)

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    676
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    New York, USA
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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; 07-07-2002 at 08:05 PM.

  • #4
    Regular Coder
    Join Date
    Jul 2002
    Location
    This little Earth.
    Posts
    383
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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).


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •