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.
Page 1 of 4 123 ... LastLast
Results 1 to 15 of 50
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to refer to a radio button?

    I can refer to a check box using its name, but i can not do this with a radio button cause they all have the same name

    So how can i refer to a radio button??

  • #2
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Either use if's reference number in the form, or use an id.

    Code:
    document.forms[formName].elements[radioButtonCollection][numberOfRadioButton]
    // or
    document.getElementById(idOfRadioButton)
    Last edited by liorean; 10-27-2003 at 01:01 AM.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Here's another way:

    <script type="text/javascript">
    <!--

    function whichradio(){ // check for a selected radio button
    formfield=document.form1.radio1 // formname and input names to be checked
    for(i=0;i<formfield.length;i++)
    if (formfield[i].checked==true){ // identify selected radio button
    itext.innerText=("You have chosen radio button "+formfield[i].value) // show result
    itext.style.color="black"
    itext.style.fontWeight="bold"
    }
    }

    // -->
    </script>


    <form name=form1>
    <P>
    <input type=radio name=radio1 value=one onclick="whichradio()">one
    <input type=radio name=radio1 value=two onclick="whichradio()">two
    <input type=radio name=radio1 value=three onclick="whichradio()">three

    <P id=itext>Which one will you choose?

    formfield[i].value is what you would use to pass
    which radio button was checked to your script.


    ....Willy

  • #4
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Why wont this work

    <html>
    <body>
    <script>
    function test22(){alert(document.getElementById('idOfRadioButton'))}
    </script>
    <input type=radio name=test id="one">
    <input type=radio name=test id="two">
    <input type=button value="get id" onClick="test22()">
    </body>


  • #5
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    Because you have no radio button with an id of "idOfRadioButton". That string should be replaced with a string to an id that you actually use.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #6
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Willy your way works fine for me
    but why


    <script type="text/javascript">

    function whichradio(){
    formfield=document.form1.radio1
    for(i=0;i<formfield.length;i++)
    if (formfield[i].checked==true){
    alert("You have chosen radio button "+formfield[i].value)
    }
    }

    </script>
    <form name=form1>
    <input type=radio name=radio1 value=help>
    <input type=radio name=radio1 value=ahh>
    <input type=button onClick=whichradio()>
    </form>


    thats what i ahve and it works
    but what is formfield.length
    and what is formfield anyways
    and why do u need the i
    and what is formfield.lenght[i]

  • #7
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    so liorean if i put in my id form a radio button it tells me in the alert window [object]

    Now why is that and how would i have to mdife my script to have it working

    thank for ur help

    thanks for urs (willy) too


  • #8
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    <html>
    <body>
    <script>
    function test22(){alert(document.getElementById("one").value)}
    </script>
    <form name=form1>
    <input type=radio name=test id="one" value=ahh>
    <input type=radio name=test id="two" value=grrff>
    <input type=button value="get id" onClick="test22()">
    <form>
    </body>


    I have tryed this again but it only gives me ahh no matter which one i select

    why

  • #9
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It works thanks to all fo you thank u very much

    Here is what i did

    <html>
    <body>
    <script>
    function test22(){
    if(document.form1.test[0].checked==true){
    alert(document.form1.test[0].value)};
    if(document.form1.test[1].checked==true){
    alert(document.form1.test[1].value)};
    if(document.form1.test[0].checked!=true && document.form1.test[1].checked!=true){alert("please select one")};};
    </script>
    <form name=form1>
    <input type=radio name=test id="one" value=ahh>
    <input type=radio name=test id="two" value=grrff>
    <input type=button value="get id" onClick="test22()">
    <form>
    </body>


  • #10
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    IGNOR THIS I AM JUST SO HAPPY I AHVE TO POST A SMILY


  • #11
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    THANKS EVERYONE

    MY SCRIPT FINALLY WORKS

  • #12
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HERES WHAT U HELP ME TO DO

    SO ITS HALF UR SCRIPT
    <HTML>
    <BODY>
    <script>

    function Reset2(){
    div.innerHTML="";
    };

    function Music(){
    if(document.FORM1.RADIO[0].checked==true){div.innerHTML+="<EMBED SRC=A.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[1].checked==true){div.innerHTML+="<EMBED SRC=B.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[2].checked==true){div.innerHTML+="<EMBED SRC=C.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[3].checked==true){div.innerHTML+="<EMBED SRC=D.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[4].checked==true){div.innerHTML+="<EMBED SRC=E.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[5].checked==true){div.innerHTML+="<EMBED SRC=F.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[6].checked==true){div.innerHTML+="<EMBED SRC=G.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[7].checked==true){div.innerHTML+="<EMBED SRC=H.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[8].checked==true){div.innerHTML+="<EMBED SRC=I.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[9].checked==true){div.innerHTML+="<EMBED SRC=J.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[10].checked==true){div.innerHTML+="<EMBED SRC=K.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[11].checked==true){div.innerHTML+="<EMBED SRC=L.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[12].checked==true){div.innerHTML+="<EMBED SRC=M.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[13].checked==true){div.innerHTML+="<EMBED SRC=N.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[14].checked==true){div.innerHTML+="<EMBED SRC=O.wma hidden=true loop=true>"};
    if(document.FORM1.RADIO[15].checked==true){div.innerHTML+="<EMBED SRC=P.wma hidden=true loop=true>"};
    };

    function Reset(){
    i=0;
    };

    function All(){
    Reset2();
    Music();
    Reset();
    };

    </script>
    <FORM NAME=FORM1>
    <INPUT TYPE=RADIO NAME=RADIO>Chic 'n' Stu<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Innervision<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Bubbles<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Boom!<BR>
    <INPUT TYPE=RADIO NAME=RADIO>NŁguns<BR>
    <INPUT TYPE=RADIO NAME=RADIO>A.D.D.<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Mr. Jack<BR>
    <INPUT TYPE=RADIO NAME=RADIO>I-E-A-I-A-I-O<BR>
    <INPUT TYPE=RADIO NAME=RADIO>36<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Pictures<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Highway Song<BR>
    <INPUT TYPE=RADIO NAME=RADIO>F**k the System<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Ego Brain<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Thetawaves<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Roulette<BR>
    <INPUT TYPE=RADIO NAME=RADIO>Streamline<BR>
    </FORM>
    <INPUT TYPE='button' VALUE='get music' onClick=All()>
    <DIV ID="div"></DIV>
    </BODY>
    </HTML>

  • #13
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts
    WORKS PERFECLY THANKS AGAIN IT MADE ME HAPPY NOW I AM HYPER AND MY MUM IS GOING INSANE

  • #14
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I'm glad you got it working.

    But, If you used this short snippet of code:
    formfield=document.form1.radio
    for(i=0;i<formfield.length;i++)
    if (formfield[i].checked==true)


    It would loop thru all of your radio buttons and choose which
    one was selected. You would not need to continually make:

    if(document.FORM1.RADIO[0].checked==true){div.innerHTML+="<EMBED SRC=A.wma hidden=true loop=true>"};

    statements.....

    You also should not name your radio buttons RADIO
    This may confuse the browser. NAME=RADIO1 would be better.

    <FORM NAME=FORM1>
    <INPUT TYPE=RADIO NAME=RADIO>Chic 'n' Stu<BR>

    ....Willy

  • #15
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    but what is formfield.length
    and what is formfield anyways
    and why do u need the i
    and what is formfield.lenght[i]
    formfield is the name of your form and radio button array
    formfield=document.form1.radio

    formfield.lenght is the number of radio buttons.
    In your case formfield.length=16

    i is your counter. You intially set the counter at 0
    for(i=0;i<formfield.length;i++)

    And, if i is less than < the formfield.length (16)
    it loops thru the radio button array and increments
    by one each time i++ until it reaches the formfield.length.

    i is the number of the radio button which was checked.
    if (formfield[i].checked==true)

    That's the best I can explain it.
    Do a google search on Javascript Loops
    if you need a more in depth explanation.

    ....Willy


  •  
    Page 1 of 4 123 ... LastLast

    Posting Permissions

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