# Track Checkbox State

## Overview

Remember whether a checkbox is clicked or not, and maintain that state on each page load as the user navigates through your site.

*Good for, e.g. a day/night mode switch.*&#x20;

## Implementing

Place this in your SITE-wide /BODY custom code area;

{% code overflow="wrap" %}

```javascript
<script>
// Handle checkbox tracking
// both checked and unchecked events 
$("input[type='checkbox'][wfu-bind-tracked]").change(function() {
  const label = $(this).attr("wfu-bind-tracked");
  if(this.checked) 
    window.tracker.track(label);
  else
    window.tracker.untrack(label);
  updateTracked();
}); 
</script>
```

{% endcode %}

Also place this code *inside* and *at the end of* the `updateTracked()` function in your site-wide /BODY custom code.&#x20;

```javascript

  // Look for any data-bound checkboxes
  // and updated them according to the tracked state
  $("input[type='checkbox'][wfu-bind-tracked]").each(function() {
    const t = $(this).attr("wfu-bind-tracked");
    const checked = window.tracker.isTracked(t) || false; 
    
    // jQuery set checkbox checked
    // https://stackoverflow.com/a/19630917
    $(this).prop('checked', checked);
    
    // For custom-styled Webflow checkboxes
    // we must manually update the checked class state 
    const $customCheck = $(this).prev(); 
    const isCustomCheck = $customCheck.length === 1;
    if (isCustomCheck) {
      if (checked)
        $customCheck.addClass("w--redirected-checked"); 
      else
        $customCheck.removeClass("w--redirected-checked"); 
    }

  });
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://attr.sygnal.com/sa5-track/webflow-cookies-and-storage-tracking/track-checkbox-state.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
