48 users online (0 members and 48 guests)  


Page 1 of 2 1 2 Last
  Results 1 to 15 of 19

Related

  1. Problem with arrays and forms    Forum: Javascript Forum
    Replies: 21
  2. Buttons and checkboxes    Forum: Javascript Forum
    Replies: 2
  3. operations with files and arrays    Forum: PHP Forum
    Replies: 4
  4. Replies: 0
  1. #1
    adamsen's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    128

    Question Checkboxes and arrays

    I need some code which can give an array some values, depending on which checkboxes which is selected.
    I think I will give these checkboxes (lets say 10 checkboxes) some values, and when you have selected the relevant checkboxes, you will have to click a button: "Create array". Then a array is created, and it will have the same number of values as there are checkboxes selected. And the values in the array will be the values of the selected checkboxes.

    If I, for instance, select checkbox number 1, 4 and 7, and I click "Create array", the content of the array will be: 1,4,7

    What will the code be for that. I donīt have the javascript code, only the HTML:
    HTML Code:
    <input type="checkbox" name="alotofcheckboxes" value="1" />
    <input type="checkbox" name="alotofcheckboxes" value="2" />
    <input type="checkbox" name="alotofcheckboxes" value="3" />
    <input type="checkbox" name="alotofcheckboxes" value="4" />
    <input type="checkbox" name="alotofcheckboxes" value="5" />
    <input type="checkbox" name="alotofcheckboxes" value="6" />
    <input type="checkbox" name="alotofcheckboxes" value="7" />
    <input type="checkbox" name="alotofcheckboxes" value="8" />
    <input type="checkbox" name="alotofcheckboxes" value="9" />
    <input type="checkbox" name="alotofcheckboxes" value="10" />
    
    <br><br>
    
    <input type="button" value="Create array" onclick=???????????

  2. #2
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Checkboxes and arrays

    Hi there adamsen,

    try it like this...
    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title></title>
    
    <style type="text/css">
    form div {
        margin:10px;
     }
    </style>
    
    <script type="text/javascript">
    if(window.addEventListener){
       window.addEventListener('load',populateArray,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',populateArray);
      }
     }
    
    function populateArray(){
    
    document.forms[0][10].onclick=function() {
       numbers=[];
    for(c=0;c<numbers.length;c++) {
       numbers[c].pop();
     }
       cb=document.getElementsByTagName('input');
    for(c=0;c<cb.length;c++) {
    if((cb[c].type=='checkbox')&&(cb[c].checked==true)){
       numbers.push(cb[c].value);
      }
     }
    if(numbers.length==0){
       numbers='empty';
     }
       alert('the array contents are...\n\n'+numbers);
      }
     }
    </script>
    
    </head>
    <body>
    
    <form action="#">
    <div>
     <input type="checkbox"  value="1">
     <input type="checkbox"  value="2">
     <input type="checkbox"  value="3">
     <input type="checkbox"  value="4">
     <input type="checkbox"  value="5">
     <input type="checkbox"  value="6">
     <input type="checkbox"  value="7">
     <input type="checkbox"  value="8">
     <input type="checkbox"  value="9">
     <input type="checkbox"  value="10">
    </div><div>
     <input type="button" value="Create array">
    </div>
    </form>
    
    </body>
    </html>
    

  3. #3
    adamsen's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    128

    Re: Checkboxes and arrays

    Thanks coothead, it was exactly what it needed.

  4. #4
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Checkboxes and arrays

    No problem, you're very welcome.

  5. #5
    adamsen's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    128

    Re: Checkboxes and arrays

    Hi again Coothead

    I have tried to insert the code in the tree you designed for me (some weeks ago), but it will not work. The idea is, that I have some of these checkboxes spread out in the tree, and in the bottom you can click the "Create Array" button, and then it will create the array. Can you change the code so thatīs possible?

  6. #6
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Checkboxes and arrays

    Hi there adamsen,
    Can you change the code so thatīs possible?
    You're wish is my command.

    Is this close to your requirements...
    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    
    <base href="http://www.coothead.co.uk/images/">
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>show/hide checkboxes</title>
    
    <style type="text/css">
    .container {
        font-family:verdana,arial,helvetica,sans-serif;
        font-size:0.9em;
        margin:4px 0;
     }
    .container img {
        width:16px;
        vertical-align:bottom;
     }
    .container span {
        font-weight:bold;
        cursor:pointer;
     }
    .off {
        display:none;
     }
    .on {
        width:100px;
        margin:10px 16px;
        padding:2px;
        border:1px solid #000;
        text-align:center;
        color:#003;
        background-color:#eef;                 
     }
    #but {
        margin:2px 0;
        background-color:#efefff; 
        border-color:#666;
     }
    </style>
    
    <script type="text/javascript">
    
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      }
     }
    
    function init(){
    
       df=document.forms[0];
       df.reset();
       
       obj=document.getElementById('wrapper');
       spn=obj.getElementsByTagName('span');
    
    for(c=0;c<spn.length;c++) { 
       document.getElementById('div'+c).className='off';
       spn[c].number=c;
    
    spn[c].onclick=function() {
       stuff(this.number);
       }
      }
    
    df[12].onclick=function() {
       numbers=[];
    for(c=0;c<numbers.length;c++) {
       numbers[c].pop();
     }
       cb=document.getElementsByTagName('input');
    for(c=0;c<cb.length;c++) {
    if((cb[c].type=='checkbox')&&(cb[c].checked==true)){
       numbers.push(cb[c].value);
      }
     }
    if(numbers.length==0){
       numbers='empty';
     }
       alert('the array contents are...\n\n'+numbers);
      }
     }
    
    function stuff(n) {
    
      dvs=obj.getElementsByTagName('div');
      ims=obj.getElementsByTagName('img');
      info=document.getElementById('div'+n);
      pic=document.getElementById('img'+n);
                                                         
    if(info.className=='on') {
       info.className='off';
       pic.src='plus.GIF'; 
     }
    else {
       info.className='on';
       pic.src='minus.GIF';                     
      }
     }
    
    </script>
    
    </head>
    <body>
    
    <div id="wrapper">
    
    <form action="#">
    
    <div class="container">
    <span><img id="img0" src="plus.GIF" alt="">checkboxes 1 - 3</span>
    <div id="div0">
     <input type="checkbox"  value="1">
     <input type="checkbox"  value="2">
     <input type="checkbox"  value="3">
    </div>
    </div>
    
    <div class="container">
    <span><img id="img1" src="plus.GIF" alt="">checkboxes 4 - 6</span>
    <div id="div1">
     <input type="checkbox"  value="4">
     <input type="checkbox"  value="5">
     <input type="checkbox"  value="6">
    </div>
    </div>
    
    <div class="container">
    <span><img id="img2" src="plus.GIF" alt="">checkboxes 7 - 9</span>
    <div id="div2">
    
     <input type="checkbox"  value="7">
     <input type="checkbox"  value="8">
     <input type="checkbox"  value="9">
    </div>
    </div>
    
    <div class="container">
    <span><img id="img3" src="plus.GIF" alt="">checkboxes 10 - 12</span>
    <div id="div3">
     <input type="checkbox"  value="10">
     <input type="checkbox"  value="11">
     <input type="checkbox"  value="12">
     <input id="but" type="button" value="create array">
    </div>
    </div>
    
    </form>
    
    </div><!-- end #wrapper -->
    
    </body>
    </html>
    

  7. #7
    adamsen's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    128

    Re: Checkboxes and arrays

    Itīs not close to my requirements, itīs exactly what looked for. Once again, thank you.

  8. #8
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Checkboxes and arrays

    Itīs not close to my requirements, itīs exactly what I looked for.
    Stone me, I must be getting good at this.

  9. #9
    adamsen's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    128

    Re: Checkboxes and arrays

    Yes, and donīt worry, I have reputated you.
    But I have one problem with your script...
    Iīve tried to move the "Create Array" button
    out of the "wrapper", so it always is displayed, but it will not work, nothing happens when I click the button.
    How can I move the button out of the "wrapper"?

  10. #10
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Checkboxes and arrays

    Hi there adamsen,

    I don't care if you move the goal posts, I'm on top of my game today.
    Code:
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    
    <base href="http://www.coothead.co.uk/images/">
    
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <meta http-equiv="Content-Style-Type" content="text/css">
    <meta http-equiv="Content-Script-Type" content="text/javascript">
    
    <title>show/hide checkboxes</title>
    
    <style type="text/css">
    .container {
        font-family:verdana,arial,helvetica,sans-serif;
        font-size:0.9em;
        margin:4px 0;
     }
    .container img {
        width:16px;
        vertical-align:bottom;
     }
    .container span {
        font-weight:bold;
        cursor:pointer;
     }
    .off {
        display:none;
     }
    .on {
        width:100px;
        margin:10px 16px;
        padding:2px;
        border:1px solid #000;
        text-align:center;
        color:#003;
        background-color:#eef;                 
     }
    #but {
        margin:2px 0 0 22px;;
        background-color:#efefff; 
        border-color:#666;
     }
    </style>
    
    <script type="text/javascript">
    
    if(window.addEventListener){
       window.addEventListener('load',init,false);
     }
    else { 
    if(window.attachEvent){
       window.attachEvent('onload',init);
      }
     }
    
    function init(){
    
       df=document.forms[0];
       df.reset();
       
       obj=document.getElementById('wrapper');
       spn=obj.getElementsByTagName('span');
    
    for(c=0;c<spn.length;c++) { 
       document.getElementById('div'+c).className='off';
       spn[c].number=c;
    
    spn[c].onclick=function() {
       stuff(this.number);
       }
      }
    
    df[12].onclick=function() {
       numbers=[];
    for(c=0;c<numbers.length;c++) {
       numbers[c].pop();
     }
       cb=document.getElementsByTagName('input');
    for(c=0;c<cb.length;c++) {
    if((cb[c].type=='checkbox')&&(cb[c].checked==true)){
       numbers.push(cb[c].value);
      }
     }
    if(numbers.length==0){
       numbers='empty';
     }
       alert('the array contents are...\n\n'+numbers);
      }
     }
    
    function stuff(n) {
    
      dvs=obj.getElementsByTagName('div');
      ims=obj.getElementsByTagName('img');
      info=document.getElementById('div'+n);
      pic=document.getElementById('img'+n);
                                                         
    if(info.className=='on') {
       info.className='off';
       pic.src='plus.GIF'; 
     }
    else {
       info.className='on';
       pic.src='minus.GIF';                     
      }
     }
    
    </script>
    
    </head>
    <body>
    
    <form action="#">
    
    <div id="wrapper">
    
    <div class="container">
    <span><img id="img0" src="plus.GIF" alt="">checkboxes 1 - 3</span>
    <div id="div0">
     <input type="checkbox"  value="1">
     <input type="checkbox"  value="2">
     <input type="checkbox"  value="3">
    </div>
    </div>
    
    <div class="container">
    <span><img id="img1" src="plus.GIF" alt="">checkboxes 4 - 6</span>
    <div id="div1">
     <input type="checkbox"  value="4">
     <input type="checkbox"  value="5">
     <input type="checkbox"  value="6">
    </div>
    </div>
    
    <div class="container">
    <span><img id="img2" src="plus.GIF" alt="">checkboxes 7 - 9</span>
    <div id="div2">
    
     <input type="checkbox"  value="7">
     <input type="checkbox"  value="8">
     <input type="checkbox"  value="9">
    </div>
    </div>
    
    <div class="container">
    <span><img id="img3" src="plus.GIF" alt="">checkboxes 10 - 12</span>
    <div id="div3">
     <input type="checkbox"  value="10">
     <input type="checkbox"  value="11">
     <input type="checkbox"  value="12">
    
    </div>
    </div>
    
    
    </div><!-- end #wrapper -->
    
    <div>
     <input id="but" type="button" value="Create array">
    </div>
    </form>
    
    </body>
    </html>
    

  11. #11
    adamsen's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    128

    Re: Checkboxes and arrays

    Thanks coothead

  12. #12
    adamsen's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    128

    Re: Checkboxes and arrays

    Hi again coothead.
    Iīve tried to make some layout in the tree-checkbox-file, but it didnīt work. (look at the attached file) I inserted some tables around the checkboxes (There wore to many to use CSS, top, left).
    But then suddenly none of the checkboxes worked!
    Can you help?
    Attached Files Attached Files

  13. #13
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Checkboxes and arrays

    Hi there adamsen,

    sorry but I do not know what it is that you are actually trying to achieve.

    You will have to give this old man some more information and/or a screen shot of how you want it to look.

  14. #14
    adamsen's Avatar
    Senior Member

    Status
    Offline
    Join Date
    Jan 2009
    Location
    Denmark
    Posts
    128

    Re: Checkboxes and arrays

    Here you have a screenshot.
    You donīt have to make what you see on the picture, just modify yhe code, so the screenshot is possible.
    Adamsen
    Attached Files Attached Files

  15. #15
    coothead's Avatar
    bald headed old fart

    Status
    Offline
    Join Date
    Aug 2003
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    732

    Re: Checkboxes and arrays

    Hi there adamsen,

    here you go...
    Attached Files Attached Files



Page 1 of 2 1 2 Last

Tags for this Thread