Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 3 of 3
  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Member #
    33622

    set/update/load cookie for checkbox with output toggle

    so I have a form with say 2 check boxes that when clicked make visible some text and and image.

    I have NO experience with cookies but I would like to make it so when you check/uncheck the box a cookie will be created that will remember the checkbox is checked and the output visible. and then I guess it has to be loaded on pageReload???

    This is my js:

    <script>
    $(".inline_collection :checkbox").click(function () {
    var hiddenVar = ".area" + this.value;
    if (this.checked) $(hiddenVar).show();
    else $(hiddenVar).hide();
    });
    </script>

    and this is my html for the checkboxeswith output:

    <li class="inline_collection">
    <label class="inline" for="1a">Complete step:</label>
    <input type="checkbox" class="show_hide" value="1a" id="1a"></input>
    <div class="area1a hidden">
    <label for="output">Step Completed! <img src="img/redCheck.png" /></label>
    </div>
    <label class="inline" for="1b">Complete step:</label>
    <input type="checkbox" class="show_hide" value="1b" id="1b"></input>
    <div class="area1b hidden">
    <label for="output">Step Completed! <img src="img/redCheck.png" /></label>
    </div>
    </li>

    Could someone please provide an example of how I would accomplish this or point me towards a good explanation online? I haven't been able to solve the issue with what I've read from various sources on stackoverflow and jsfiddle.

  2.  

  3. #2
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Member #
    33622
    so I've got it working so it creates and reads a cookie for all the checkboxes. and then i use inline collection to toggles divs containing certain images on and off using the check box and this works. my issue is the checkboxes will repoplulate but the divs that usually become visible when the checkbox is checked don't when it loads the checkbox cookie.


    <script>
    $(":checkbox").on("change", function(){
    var checkboxValues = {};
    $(":checkbox").each(function(){
    checkboxValues[this.id] = this.checked;
    });



    $.cookie('checkboxValues', checkboxValues, { expires: 365, path: '/' })
    });




    function repopulateCheckboxes(){
    var checkboxValues = $.cookie('checkboxValues');
    if(checkboxValues){
    Object.keys(checkboxValues).forEach(function(eleme nt) {
    var checked = checkboxValues[element];
    $("#" + element).prop('checked', checked);
    });
    }
    }

    $.cookie.json = true;
    repopulateCheckboxes();




    $(".inline_collection :checkbox").click(function () {
    var hiddenVar = ".area" + this.value;
    if (this.checked) $(hiddenVar).show();
    else $(hiddenVar).hide();
    });
    </script>


    i've tried adding a .on validate function after all the other javascript but that doens't work either.

    $(".inline_collection :checkbox").on(function validate() {
    if (document.getElementById().checked) {
    $(hiddenVar).show();
    } else {
    $(hiddenVar).hide();
    }
    });


    i've also tried placing randitions of the inline_checkbox.click)function... within the :checkbox.on ("change... at the start of the srcipt and within the function repopulateCheckboxes() . but no avail.

    any ideas on how to get the hidden areas for checked boxes to appear after the checkboxes have been repopulated?

  4. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    10
    Member #
    33622
    solved it! here is the solution.

    <script>
    $(":checkbox").on("change", function(){
    var checkboxValues = {};
    $(":checkbox").each(function(){
    checkboxValues[this.id] = this.checked;
    });


    $.cookie('checkboxValues', checkboxValues, { expires: 365, path: '/' })
    });


    function repopulateCheckboxes() {
    var checkboxValues = $.cookie('checkboxValues');
    if(checkboxValues){
    Object.keys(checkboxValues).forEach(function(eleme nt) {
    var checked = checkboxValues[element];
    var jMatchingCheckbox = $(":checkbox[id='"+element+"']");
    if (jMatchingCheckbox) {
    jMatchingCheckbox.prop('checked', checked);
    $(".area" + jMatchingCheckbox.attr('id')).toggle(checked);
    }
    });
    }
    }




    $.cookie.json = true;
    repopulateCheckboxes();



    $(".inline_collection :checkbox").click(function () {
    var hiddenVar = ".area" + this.value;
    if (this.checked) $(hiddenVar).show();
    else $(hiddenVar).hide();
    });
    </script>


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 09:28 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com