Checkboxes from an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rupak
    New Member
    • Jan 2008
    • 25

    Checkboxes from an array

    i want to chech thoes chekboxes which are sored in an array,

    i.e. there is an array PreviousItems[] = [col1,col2,,col4 ,,,col7,,,,col1 1,col12,,]

    want to checked thoes checkboxes in the form whoes id are stored in array PreviousItems.

    i.e.
    [HTML]<input type='checkbox' name='coffee' id='col1' value='Unit' checked />
    <input type='checkbox' name='coffee' id='col2' value='Unit' checked />
    <input type='checkbox' name='coffee' id='col3' value='Unit' unchecked />
    <input type='checkbox' name='coffee' id='col4' value='Unit' checked />
    <input type='checkbox' name='coffee' id='col5' value='Unit' unchecked />

    [/HTML]Simillarly
    Last edited by acoder; Feb 22 '08, 09:06 AM. Reason: Added code tags
  • hsriat
    Recognized Expert Top Contributor
    • Jan 2008
    • 1653

    #2
    Originally posted by rupak
    i want to chech thoes chekboxes which are sored in an array,

    i.e. there is an array PreviousItems[] = [col1,col2,,col4 ,,,col7,,,,col1 1,col12,,]

    want to checked thoes checkboxes in the form whoes id are stored in array PreviousItems.

    i.e.
    <input type='checkbox' name='coffee' id='col1' value='Unit' checked />
    <input type='checkbox' name='coffee' id='col2' value='Unit' checked />
    <input type='checkbox' name='coffee' id='col3' value='Unit' unchecked />
    <input type='checkbox' name='coffee' id='col4' value='Unit' checked />
    <input type='checkbox' name='coffee' id='col5' value='Unit' unchecked />

    Simillarly

    [CODE=javascript]
    function checkPreviousIt ems() { //call this where you need
    for (var i=0; i<PreviousItems .length; i++)
    document.getEle mentById(Previo usItems[i]).checked = true;
    }[/CODE]

    Comment

    • rupak
      New Member
      • Jan 2008
      • 25

      #3
      Actually i want to store the checkboxes selected on Save button, and on modification of cehckxboxes want to Cancle so the previous set will be checked again, means previous condition will be retrived.

      Code:
      <script language="javascript">
      var ids = [];
      var preids = [];
      var PreviousItems= new Array();
      function cancel()
      {
      for (var i=0; i<arry.length; i++) 
      document.tcol.getElementById(PreviousItems[i]).checked = true;
      }
      
      function show()
      {
      
      txt="";
      btn="";
      ids=[];
      preids=[];
      coffee=document.forms['tcol'].coffee;
      for (i=0;i<coffee.length;++ i)
      {
       if (coffee[i].checked)
      {
      txt=txt + coffee[i].id;
      btn = 't'+ coffee[i].id;
      prebtn = coffee[i].id;
      ids[i]=btn;
      preids[i]=prebtn;
      }
      }
      PreviousItems = preids;
      }
      </script>
      
      <input type='checkbox' name='coffee' id='col1' value='Unit1' checked />
      <input type='checkbox' name='coffee' id='col2' value='Unit2' checked />
      <input type='checkbox' name='coffee' id='col3' value='Unit3' checked />
      <input type='checkbox' name='coffee' id='col4' value='Unit4' checked />
      <input type='checkbox' name='coffee' id='col5' value='Unit5' checked />
      <input type='checkbox' name='coffee' id='col6' value='Unit6' checked />
      <input type='checkbox' name='coffee' id='col7' value='Unit7' checked />
      
      <p><input type='button' onclick='show()' value='Save'> <input type='button' onclick='cancel()' value='Cancel'></p>

      Comment

      • hsriat
        Recognized Expert Top Contributor
        • Jan 2008
        • 1653

        #4
        Try the following changes..
        line 8: document.getEle mentById(Previo usItems[i]).checked = true;
        line 34 onwards: <input type='checkbox' name='coffee[]' id='col1' value='Unit1' checked />

        And I'm sorry I didn't get your algorithm.

        Comment

        • rupak
          New Member
          • Jan 2008
          • 25

          #5
          Originally posted by hsriat
          Try the following changes..
          line 8: document.getEle mentById(Previo usItems[i]).checked = true;
          line 34 onwards: <input type='checkbox' name='coffee[]' id='col1' value='Unit1' checked />

          And I'm sorry I didn't get your algorithm.

          Actually there are lots of checkboxes which are default checked and then have two buttons
          SAVE and CANCEL

          Once the user changed the present condition of the checkboxes it need to be stored, thus on SAVE we store the modified condition in an array.

          Else if user modified the present condition wrongly and then want to keep the previous one he need to CANCEL it. And then do the correct modification and SAVE them.

          According to the SAVED value we have to print the checkboxes Value

          Comment

          • hsriat
            Recognized Expert Top Contributor
            • Jan 2008
            • 1653

            #6
            [CODE=javascript]var txt;
            var ids[];
            var PreviousItems= new Array();
            function cancel() {
            for (var i=0; i<PreviousItems .length; i++)
            document.getEle mentById(Previo usItems[i]).checked = true;
            }

            function show() {
            txt="";
            ids = [];
            coffee=document .tcol.coffee; //are you sure its forms['tcol'] and not tcol?
            for (var i=0;i<coffee.le ngth;++ i) {
            if (coffee[i].checked) {
            txt += coffee[i].id; //is this really needed?
            var temp = ids.push('t'+ coffee[i].id);
            temp = PreviousItems.p ush(coffee[i].id);
            }
            }
            }[/CODE]

            Also change the names of checkboxes from coffee to coffee[ ]

            Comment

            • rupak
              New Member
              • Jan 2008
              • 25

              #7
              Thanks it has worked out.

              Comment

              • hsriat
                Recognized Expert Top Contributor
                • Jan 2008
                • 1653

                #8
                Originally posted by rupak
                Thanks it has worked out.

                Good to hear that. :)

                Post again in case of any other problem.

                Comment

                Working...