How to select checkbox using localStorage value

3707 views javascript
6

I am using Checkboxes the value is consider a Dropdown box.

<div class="form-group" id="permission">
    <label> Permission : </label>
    <input type="checkbox" name="permission" id="pRead" value="pread" > Read 
    <input type="checkbox" name="permission" id="pWrite" value="pwrite"> Write 
    <input type="checkbox" name="permission" id="pDelete" value="pdelete" > Delete 
<div>

answered question

please provide more information. Please provide some code you have tried yourself.

Do you have any problems with code you've written? If so, please show that code. If your question is 'how do I programmatically check a checkbox' then there's thousands of answers to that already if you take the time to search.

I don't find any question~

@kit Take a look at the title.

So that's mean he want us to suggest a solution for him?

2 Answers

0

Try this.

$('input[name="permission"][value="' + value + '"]:checkbox"').prop('checked',true);

"value" will be your local storage value

posted this
2

I have some code which remembers my checkbox states which I'm trying to change so that it stores the state of each checkbox in localstorage and recalls it on document load, so that even if you close the browser it will still remember the state of each checkbox.

$(document).ready(function() { 
    // Set aside the original state of each checkbox.
    $("input.toggle").each(function() {
        $(this).data("originallyChecked", $(this).is(":checked"));
        localStorage.setItem(chkbox, originallyChecked);
    }); 
    // Check whether it really changed on click.
    $("input.toggle").change(function() {
        var action = $(this).siblings("span");          
    });
}); 
    $(document).ready(function() {
         $("input#select-all").click(function() {
         $("input.toggle").attr('checked', this.checked);
        $("input.toggle").change();
     });
});

posted this

Have an answer?

JD

Please login first before posting an answer.