Save a toogle state to Local Storage

by Akram kamal

HTML

<table style="width:100%" border="1">
    <tr>
        <td>Row 1</td>
    </tr>
    <tr>
        <td>Row 2</td>
    </tr>
    <tr>
        <td>Row 3</td>
    </tr>
</table>

CSS

.highlight {
    color: blue;
    font-weight: bold;
}

JavaScript

var highlights = JSON.parse(localStorage.highlight || "[]")
$("table tr").click(function() {
    $(this).toggleClass("highlight");
    localStorage.highlight = JSON.stringify(
        $("table tr").map(function() {
            return $(this).hasClass("highlight")
        }).get()
    )

});
if (highlights.length)
    $("table tr").each(function(i) {
        $(this).toggleClass("highlight", highlights[i])
    });