Results 1 to 3 of 3

Thread: Uncheck all checkboxes and clear localStorage at the same time.

  1. #1
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default Uncheck all checkboxes and clear localStorage at the same time.

    I think I am close to where I want to be, but I still need help. How can I uncheck all checkboxes and clear localStorage for those checkboxes at the same time? Right now it unchecks them, but localStorage isn't actually cleared:

    http://jsfiddle.net/T5Pm7/233/

    Code:
    //Adds to LocalStorage
    
    (function() {
        var boxes = document.querySelectorAll("input[type='checkbox']");
        for (var i = 0; i < boxes.length; i++) {
            var box = boxes[i];
            if (box.hasAttribute("store")) {
                setupBox(box);
            }
        }
       
        function setupBox(box) {
            var storageId = box.getAttribute("store");
            var oldVal    = localStorage.getItem(storageId);
            console.log(oldVal);
            box.checked = oldVal === "true" ? true : false;
           
            box.addEventListener("change", function() {
                localStorage.setItem(storageId, this.checked);
            });
        }
    })();
    Code:
    //Uncheck all
    
    document.getElementById('selections').addEventListener('change', function(e) {
            var el = e.target;
            var inputs = document.getElementById('selections').getElementsByTagName('input');
           
            if (el.id === 'all') {
           
                if (el.checked) {
               
                    for (var i = 1, input; input = inputs[i++]; ) {
                        input.checked = false;
                    }
                }
            }
           
            else {
                inputs[0].checked = false;
            }
        }, false);
    Code:
    //Checkbox selections
    
    
    (function(d) {
    
        function nodeFlush(e) {
            while (e.firstChild) e.removeChild(e.firstChild);
        }
    
        function nodeAdd(e, node) {
            e.appendChild(typeof node == 'object' ? node : d.createTextNode(node));
        }
    
        function nodeReplace(e, node) {
            nodeFlush(e);
            nodeAdd(e, node);
        }
    
        function eventAdd(e, eventName, handler) {
            if (e.addEventListener) e.addEventListener(eventName, handler, false);
                else e.attachEvent('on' + eventName, handler);
        }
    
        var
            target = d.getElementById('selections'),
            output = d.getElementById('selectionsResults'),
            inputs = target.getElementsByTagName('input');
    
        function checkInputs() {
            var result = [];
            for (var i = 0; i < inputs.length; i++) {
                if (inputs[i].checked) result.push(inputs[i].value);
            }
            nodeReplace(output, result.length ? result.join(', ') : 'nothing');
                document.getElementById('selectionsResults').innerHTML=document.getElementById('selectionsResults').textContent;
    
        }
    
        for (var i = 0; i < inputs.length; i++) {
            eventAdd(inputs[i], 'change', checkInputs);
            eventAdd(inputs[i], 'click', checkInputs);
           
        }
    
        checkInputs();
    
    })(document);
    Last edited by qwikad.com; 09-08-2015 at 04:46 PM.

  2. #2
    Join Date
    Sep 2007
    Location
    The Netherlands
    Posts
    1,881
    Thanks
    49
    Thanked 266 Times in 258 Posts
    Blog Entries
    56

    Default

    Just add localStorage.clear()
    Code:
    //Uncheck all
    
    document.getElementById('selections').addEventListener('change', function(e) {
            var el = e.target;
            var inputs = document.getElementById('selections').getElementsByTagName('input');
            
            if (el.id === 'all') {
            
                if (el.checked) {
                
                    for (var i = 1, input; input = inputs[i++]; ) {
                        input.checked = false;
    		localStorage.clear();
                    }
                }
            }
            
            else {
                inputs[0].checked = false;
            }
        }, false);

  3. #3
    Join Date
    Oct 2012
    Posts
    180
    Thanks
    22
    Thanked 1 Time in 1 Post

    Default

    Great, thank you!

Similar Threads

  1. fluidtextresizer localstorage ?
    By liat in forum Dynamic Drive scripts help
    Replies: 10
    Last Post: 07-13-2015, 07:02 PM
  2. Resolved Uncheck a checkbox if another is checked (3 checkboxes)
    By qwikad.com in forum JavaScript
    Replies: 8
    Last Post: 04-24-2014, 08:07 PM
  3. How to uncheck-disable, remaing checkboxes
    By gootynz in forum JavaScript
    Replies: 0
    Last Post: 01-24-2012, 02:00 PM
  4. Cookie script to LocalStorage?
    By pxlcreations in forum JavaScript
    Replies: 19
    Last Post: 07-10-2010, 09:26 PM
  5. More help (this time more clear)
    By Fewmitz in forum JavaScript
    Replies: 0
    Last Post: 03-30-2007, 02:17 AM

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
  •