Loop over multidimension radio button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nurul02
    New Member
    • Apr 2010
    • 7

    Loop over multidimension radio button

    In my page there is a multidimension radiobutton groups

    below is my code:
    Code:
    <input type="radio" id="as_mark1" name="as_mark1" value="1">
    <input type="radio" id="as_mark1" name="as_mark1" value="2">
    <input type="radio" id="as_mark1" name="as_mark1" value="3">
    
    <input type="radio" id="as_mark2" name="as_mark2" value="1">
    <input type="radio" id="as_mark2" name="as_mark2" value="2">
    <input type="radio" id="as_mark2" name="as_mark2" value="3">
    
    <input type="button" id="startx" name="startx" value="Calculate" onclick="javascript:sumitup()" />
    at this moment, i give only static value to get all the checked radio button,

    var xax = [document.forms. f1.as_mark1,as_ mark2];

    my question is,how do I make it dynamic if there is more than 2 rows of radio button(eg:as_ma rk3,as_mark4 etc) at this moment,it is working fine if i put static as below
    var xax = [document.forms. f1.as_mark1,as_ mark2];

    i need to make it dynamic since i will not know how many as_mark will I obtain in my page
    Last edited by Dormilich; May 3 '10, 05:38 AM. Reason: please use [code] tags when posting code
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    you cannot easily find out, how many named input groups you have (you could, of course, get all input elements and check for their type and name attributes).

    besides, IDs must be unique!

    Comment

    • nurul02
      New Member
      • Apr 2010
      • 7

      #3
      Originally posted by Dormilich
      you cannot easily find out, how many named input groups you have (you could, of course, get all input elements and check for their type and name attributes).

      besides, IDs must be unique!
      The id of each group is already unique.
      I named 'as_mark1' as 1st group of radio button, and 'as_mark2' as for the 2nd group of the radio button. I've tried to construct a loop :

      Code:
      total=2;//the value for var total is obtained from the database
      var scripts = new Array();
      for(h=1;h<total;h++)
      {
      	[B]var kk="as_mark"+h;//this line is not working, not sure if the append part cause the error[/B]
      	var k=document.forms.f1.kk;
      	scripts.push(k);
      }
      //by right it should produce var xax=[document.forms.f1.as_mark1,document.forms.f1.as_mark2)
      var xax = scripts;
      I am not sure if my idea is correct,anyone mind to lend some help?please..
      Last edited by Dormilich; May 3 '10, 07:41 AM. Reason: Please use [code] tags when posting code

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        The id of each group is already unique.
        an ID must be unique per Element.
        Code:
        	[B]var kk="as_mark"+h;//this line is not working, not sure if the append part cause the error[/B]
        	var k=document.forms.f1.kk;
        that’s right, the Error Console should show the error there (you need to pass the input names as variable:)
        Code:
        var k=document.forms.f1[B][kk][/B]

        Comment

        • nurul02
          New Member
          • Apr 2010
          • 7

          #5
          Originally posted by Dormilich
          an ID must be unique per Element.

          that’s right, the Error Console should show the error there (you need to pass the input names as variable:)
          Code:
          var k=document.forms.f1[B][kk][/B]
          Dormilich ,
          thank u..u have helped me solved it..ive been seeking for this solution more than 1 week..gosh.. i never know that only the bracket [] could solve everything..tha nk u so much dorm!!

          Comment

          • nurul02
            New Member
            • Apr 2010
            • 7

            #6
            anyone,
            again this time i need to validate the checked radio button. how do i check if each row of radio button has been checked.say i have below design

            Code:
            <input type="radio" id="as_mark1" name="as_mark1" value="1">
            <input type="radio" id="as_mark1" name="as_mark1" value="2">
            
            <input type="radio" id="as_mark2" name="as_mark2" value="1">
            <input type="radio" id="as_mark2" name="as_mark2" value="2">
            basically there's two group of radio button(as_mark1 ,as_mark2),but the number of radio button group could be dynamic as ive told earlier.

            ive used ur previous idea but seems that it is not working at all =(..
            below are my codes..

            Code:
            total =2;
            for(h=1;h<total;h++)
            {
            	var kk="as_mark"+h;
            	var k=document.forms.f1[kk];
             
                 [B]  if(!k.checked){//this line throws error[/B]
                    alert("You havent checked radio button:"+h)
                    }
            }
            anybody mind to tell me y is it throwing the error??
            Last edited by Dormilich; May 6 '10, 03:46 PM. Reason: Please use [code] tags when posting code

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              a link to the page would be practical.

              besides, you have duplicate IDs again.

              Comment

              Working...