SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help with checkbox

    I have some code like:

    <td><label>Vokalinput type="checkbox" onclick="javascript:convert_vokal()" id="vokal"/></label></td>

    The convert_vokal() changes a value vokal to true or false according to the state of the checkbox.

    I have now implemented cookies which means the default value of vokal could be true. The checkbox should then be selected as default. How can i visually set the box being checked if vokal=true and not checked is vokal=false?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    <script type="text/javascript">
    
    function Set(){
     var args=Set.arguments,x,s,z0=1,z0a;
     for (;z0<args.length;z0++){
      x=document.forms[0][args[z0]];
      if (x){
       s='';
       if (x.length){
        for (z0a=0;z0a<x.length;z0a++){
         s+=x[z0a].checked+'#';
        }
       }
       else {
        s=x.checked+'#';
       }
       document.cookie=args[z0]+'='+s+';expires='+(new Date(new Date().getTime()+args[0]*86400000).toGMTString())+';path=/';
      }
     }
    }
    
    function GetCookie(){
     var ary=['tom','joe','jill'],re,rtn,z0=0,z0a;
     for (;z0<ary.length;z0++){
      x=document.forms[0][ary[z0]];
      if (x){
       re=new RegExp(ary[z0]+'=[^;]+','i');
       rtn=(document.cookie.match(re)?document.cookie.match(re)[0].split("=")[1]:'').split('#');
       if (x.length){
        for (var z0a=0;z0a<rtn.length-1;z0a++){
         x[z0a].checked=rtn[z0a]=='true'?true:false;
        }
       }
       else {
         x.checked=rtn[0]=='true'?true:false;
       }
      }
     }
    }
    
    if (window.addEventListener){
     window.addEventListener('load',GetCookie, false);
    }
    else if (window.attachEvent){
     window.attachEvent('onload',GetCookie);
    }
    
    /*]]>*/
    </script>
    
    </script>
    
    </head>
    
    <body>
    <form onclick="Set(1,'tom','joe','jill');">
    <input type="checkbox" name="tom" /><input type="checkbox" name="tom" />
    <br />
    <input type="checkbox" name="joe" />
    <br />
    <input type="radio" name="jill" /><input type="radio" name="jill" /><input type="radio" name="jill" /><input type="radio" name="jill" />
    </form>
    </body>
    
    </html>

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, this is too complicated for a newbie like me. I'm impressed about what I think your code does and a can see I could probably use most of it in replace for the code I already use. If you have got some time left please take a look at my code at www.limesurvey.no/ord/index3.html

    Your cookiecode looks very interesting if I could adapt it to my project. If you could advise how I could include your code in my own I would be very happy. If not, how can I adapt yout code? I really don't understand much of it, I am afraid :-)

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Portsmouth UK
    Posts
    1,497
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First you are calling function readCookie before it is available so you must place it before

    var uppercase = (readCookie("lesetreneren_uppercase") == "true");
    var vokal = (readCookie("lesetreneren_vokal") == "true");
    var use_random = (readCookie("lesetreneren_random") == "true");

    second

    to use my solution

    your form elements must be given names and must be nested in a form as in my example


Bookmarks

Posting Permissions

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