RadioButtonList

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • moonalamoor
    New Member
    • Feb 2008
    • 18

    RadioButtonList

    Hi

    I have an Evaluation Page
    at the end I want to submit this Evaluation

    But before that i want to check if every RadioButtonList in the page is checked

    How can i do that

    shall I do (IF Statement)
    but i have 20 Question
    so ... i will do for them all !!!!
    or there is another way to check them all ??
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    You can try this code

    Code:
    <script language = "javascript">
    
    function CheckRadioList()
     {
      var grp = document.forms[0].elements["BtnList1"];
      var x, len = grp.length;
     for (x=0; x<len; x++) 
       {
        if (!grp[x].checked) break;
        return true;
      }
      if (x < len) 
       {
        window.alert("radio button not checked.");
        return false;
       }
    
    </script>
    Originally posted by moonalamoor
    Hi

    I have an Evaluation Page
    at the end I want to submit this Evaluation

    But before that i want to check if every RadioButtonList in the page is checked

    How can i do that

    shall I do (IF Statement)
    but i have 20 Question
    so ... i will do for them all !!!!
    or there is another way to check them all ??

    Comment

    • moonalamoor
      New Member
      • Feb 2008
      • 18

      #3
      Than you a lot

      i understand the idea

      but how can i write this variable

      var grp = document.forms[0].elements["BtnList1"];


      this is not available
      document.forms[0].elements["BtnList1"];

      it gives me an error

      Comment

      • jeffstl
        Recognized Expert Contributor
        • Feb 2008
        • 432

        #4
        What is the name given to your radio buttons in the form?

        You need to make it the same name as referred to in the javascript :-$

        <input type=radio name="btnlist1" >

        If ALL your radio buttons have the same name you will have created an array, which will work with the code given above.

        You need to loop through this array in some way shape or form, whether its with javascript or with vbscript using the submitted data from the form.

        That is how you can check them all.

        Comment

        • markrawlingson
          Recognized Expert Contributor
          • Aug 2007
          • 346

          #5
          Originally posted by moonalamoor
          Than you a lot

          i understand the idea

          but how can i write this variable

          var grp = document.forms[0].elements["BtnList1"];


          this is not available
          document.forms[0].elements["BtnList1"];

          it gives me an error
          Radio buttons are indice based because as jeffstl said they're basically an array of values if all of the buttons have the same object name.
          So you have to refer to them by their indice.

          document.forms[0].elements['BtnList1'] is just an object, it has no value. To get a value, or whether an item is checked or not, you need to refer to the particular item in the radio list array.

          EG:
          [code=javascript]
          document.forms[0].elements['BtnList1'](0).value;
          document.forms[0].elements['BtnList1'](0).checked;
          //etc
          //That will refer specifically to the first radio button within the BtnList1 radio button group and refer to its value and whether it is checked or not, respecively.
          [/code]

          Save the code below to a new .html file and run it in your browser to get a better feel of this.

          [code=html]
          <script>
          function doCheck(i) {
          alert(document. forms[0].elements['BtnList1'](i).value + ' ' + document.forms[0].elements['BtnList1'](i).checked);
          }
          </script>
          <form name="whatever" >
          <input type="radio" name="BtnList1" value="0" onclick="doChec k(0)">
          <input type="radio" name="BtnList1" value="1" onclick="doChec k(1)">
          <input type="radio" name="BtnList1" value="2" onclick="doChec k(2)">
          <input type="radio" name="BtnList1" value="3" onclick="doChec k(3)">
          <input type="radio" name="BtnList1" value="4" onclick="doChec k(4)">
          <input type="radio" name="BtnList1" value="5" onclick="doChec k(5)">
          <input type="radio" name="BtnList1" value="6" onclick="doChec k(6)">
          </form>
          [/code]

          Sincerely,
          Mark

          Comment

          • moonalamoor
            New Member
            • Feb 2008
            • 18

            #6
            Hi
            Thank you all I understand what you said and I try it

            But I want to use RadioButtonList
            Not <input type="radio" name="BtnList1" >

            Because my page will be for evaluation
            Then for every Quest there is many choises.. so I used RadioButtonList

            I tried to do what you tell me.. but I get some error
            Becouse I don’t know how to creat an array for RadioButtonList
            And do (loop to check them)


            OR I must use input type="radio" to use arry ???

            Comment

            • markrawlingson
              Recognized Expert Contributor
              • Aug 2007
              • 346

              #7
              Ah i see, so you're using .net - You have to be specific of the technologies you're using. ASP is not asp.net - they are very different and the experts in this forum are proficient with classic asp, we generally know little about .net programming - so you'll find it difficult to get the help you require.

              However, I know enough to fake my way around a bit.. so I'll try to give you a hand.

              You'll need to do something like: This is of course in Visual Basic.

              [code=asp]
              Sub CheckRadio(s As Object, e As EventArgs)
              If Request.Form("B tnList1") Is Nothing Or Request.Form("B tnList1") = "" Then
              YourLabel.Text = "You did not select an item from the radio button list control!"
              Else
              YourLabel.Text = "You selected the following... " & BtnList1.Select edIndex.Text
              End If
              End Sub
              [/code]

              Yes, you'll have to do that for all of them. Or, you can try something like this but i'm not entirely certain how this will work in .net

              [code=asp]
              Sub CheckRadio(s As Object, e As EventArgs)
              For Each oItem As Object In Request.Form
              If Request.Form(oI tem) Is Nothing Or Request.Form(oI tem) = "" Then
              YourLabel.Text = "You did not select an item from the radio button list control!"
              Else
              YourLabel.Text = "You selected the following... " & oItem.SelectedI ndex.Text
              End If
              Next
              End Sub
              [/code]

              And call this sub onSubmit. Or if you want to test it try calling it OnClick of the radiolistcontro l to see what you get.

              Here is a reference: (look at the displayMessage( ) Sub)



              If this still doesn't help.. I will have a mod move this thread to the .net forums where the resident experts there will be able to help you more than we can.

              Sincerely,
              Mark


              Originally posted by moonalamoor
              Hi
              Thank you all I understand what you said and I try it

              But I want to use RadioButtonList
              Not <input type="radio" name="BtnList1" >

              Because my page will be for evaluation
              Then for every Quest there is many choises.. so I used RadioButtonList

              I tried to do what you tell me.. but I get some error
              Becouse I don’t know how to creat an array for RadioButtonList
              And do (loop to check them)


              OR I must use input type="radio" to use arry ???

              Comment

              • moonalamoor
                New Member
                • Feb 2008
                • 18

                #8
                Hi ...

                I don't understand what you do :S

                but that what i did (as you told me)
                and it works :)


                <script>
                function doCheck() {

                var i , ch= -1 ;
                for (i=0; i<5 && ch==-1 ; i++)
                {
                if(document.for ms[0].elements['BtnList1'](i).checked)
                {
                ch=1;
                alert("You checked "+document.form s[0].elements['BtnList1'](i).value+" "+ch);
                }
                else
                {
                alert("You DON'T checked "+document.form s[0].elements['BtnList1'](i).value+" "+ch);
                }
                }

                if(ch== -1)
                {
                ("warning an exsist (label) by red")
                }
                }
                </script>




                what i want know

                Q: How to change the (color to red) of label of the question that is unchecked !!
                coz labels is undefined in my script !!! i dont't know why??

                and what type of script that i have to use according to this script ??


                thanks again

                Comment

                • markrawlingson
                  Recognized Expert Contributor
                  • Aug 2007
                  • 346

                  #9
                  I'm not sure how to do this in .net but I'm thinking it's probably something like..

                  [code=asp]
                  If Request.Form("B tnList1") Is Nothing Or Request.Form("B tnList1") = "" Then
                  LabelControl.Co lor = 'red'
                  End If
                  [/code]

                  In javascript, it's:

                  [code=javascript]
                  document.getEle mentById('Label tocolorRed').st yle.color = 'red'
                  [/code]

                  Sincerely,
                  Mark

                  Originally posted by moonalamoor
                  Hi ...

                  I don't understand what you do :S

                  but that what i did (as you told me)
                  and it works :)


                  <script>
                  function doCheck() {

                  var i , ch= -1 ;
                  for (i=0; i<5 && ch==-1 ; i++)
                  {
                  if(document.for ms[0].elements['BtnList1'](i).checked)
                  {
                  ch=1;
                  alert("You checked "+document.form s[0].elements['BtnList1'](i).value+" "+ch);
                  }
                  else
                  {
                  alert("You DON'T checked "+document.form s[0].elements['BtnList1'](i).value+" "+ch);
                  }
                  }

                  if(ch== -1)
                  {
                  ("warning an exsist (label) by red")
                  }
                  }
                  </script>




                  what i want know

                  Q: How to change the (color to red) of label of the question that is unchecked !!
                  coz labels is undefined in my script !!! i dont't know why??

                  and what type of script that i have to use according to this script ??


                  thanks again

                  Comment

                  • moonalamoor
                    New Member
                    • Feb 2008
                    • 18

                    #10
                    OK ....
                    i think that i'am using ( asp ) not ( .net ) :S

                    i'm doing a web site ... with ( HTML)
                    and script with javascript (as you told me) :)


                    i used this, coz the first one i didn't understant it

                    Code: ( javascript ):

                    document.getEle mentById('Label 1').style.color = 'red'

                    it works
                    but the label ... return to black after one second ... I don't know why :S
                    How can i keep red ... ???



                    Thank you :)

                    Comment

                    • markrawlingson
                      Recognized Expert Contributor
                      • Aug 2007
                      • 346

                      #11
                      That's wierd :P

                      Show your code so we can help determine why it's changing back to black.

                      Sincerely,
                      Mark

                      Originally posted by moonalamoor
                      OK ....
                      i think that i'am using ( asp ) not ( .net ) :S

                      i'm doing a web site ... with ( HTML)
                      and script with javascript (as you told me) :)


                      i used this, coz the first one i didn't understant it

                      Code: ( javascript ):

                      document.getEle mentById('Label 1').style.color = 'red'

                      it works
                      but the label ... return to black after one second ... I don't know why :S
                      How can i keep red ... ???



                      Thank you :)

                      Comment

                      • moonalamoor
                        New Member
                        • Feb 2008
                        • 18

                        #12
                        i thing that i want to do some thing in page load :S
                        i don't know


                        this is my script code put i put it before ...

                        Code:
                        function doCheck() {
                        
                        var i ,j, ch ;
                        
                        for (j=1; j<21 ; j++) 
                        { 
                        ch= -1;
                        for (i=0; i<5 && ch==-1 ; i++) 
                          {       
                          if(document.forms[0].elements['BtnList'+j](i).checked)
                          {
                            ch=1;
                            alert("You checked "+document.forms[0].elements['BtnList'+j](i).value+" "+ch+"  BtnList"+j);
                          }
                         else
                            {
                            alert("You DON'T checked "+document.forms[0].elements['BtnList'+j](i).value+"  "+ch+"  BtnList"+j);
                            }
                          }
                          
                          if(ch==-1)
                          {
                            document.getElementById("Label"+j).style.color= 'red';
                            alert("Please answer Question number "+j);
                            
                          }}}

                        Comment

                        • moonalamoor
                          New Member
                          • Feb 2008
                          • 18

                          #13
                          Hi

                          ummmm

                          just want to know

                          how can i control the page loading

                          coz when the script finish itss jop

                          the page will reset automatically (i don't want that) .... !!!!

                          i want to go to another event (on server_side) after an event (on_client side) finish successfully .... so how can i do that ???

                          Comment

                          Working...