How to sync the states of two identical checkboxes?

Hello dear people,

Is there a way to sync the states of two checkboxes in Webflow so that both visually reflect the same checked or unchecked state? The idea is to later hide one checkbox but still see the same results when unhiding the checkbox.

Many thanks in advance!

Here is my public share link: LINK
(how to access public share link)

Yes, you can keep them in sync with javascript.
Detect a change event on checkbox 1, and then update checkbox 2 to match.

Thanks for your great help! I’m not an expert in javascript but from what you suggested I tried this:

      const checkbox1 = document.querySelector('#checkbox1');
      const checkbox2 = document.querySelector('#checkbox2');

      checkbox1.addEventListener('change', () => {
        checkbox2.checked = checkbox1.checked;

Unfortunately it didn’t work out. What am I doing wrong?

Yep that’s basically right. Share your read-only site link, and your published site link where you have this implemented and I have have a look when I’m free.

I added a new extended javascript. Now the behavior is in sync but the visual properties are not.

@memetican Thanks for your time and help!

Hi @memetican

Thank you very much for the helpful video and explanation. I appreciate it a lot! I think you have pointed me in the right direction and now I need some time to figure out the solution.

Indeed, the basis of the checkbox was a component from Finsweet that I converted for my purposes. This is probably where I need to start, as you mentioned.

The end goals:

  • Different filter layouts for desktop and mobile, but for that they need to be synced.
  • I am trying out some layout options where the filter options reveal on hover (and close on hover-out), but can also be pinned to show permanently (eye icon). My understanding is that I will need two identical checkboxes for this.
  • Get a basic understanding of how I can pull checkbox selections from other sites in the future.

Again thanks a lot for your help. I’ll let you know if I need any more precious help from you.

There are a lot of ways to change your layout for mobile. The approach you’re taking seems the most complex, since it requires you to script “synchronized” filter UIs.

Here are three alternatives;

Since they’re CSS-based, a manual CSS grid allows you to layout those elements in a very different way for your mobile view, without duplicating those elements.

Duplicate that whole filter & results portion of the page, and then make one region desktop-only, and the other mobile-only. It will save you the scripting and give you unlimited creativity in your layout of both the filter portion, and the results.

The nav would direct desktop users to the desktop version, and mobile users to the mobile version. Other than that, the sites would look and behave identically.