0

this might be a hard one or a really obvious one.

What I did until now.

I have built a form and inside that form, there are 21 checkboxes divided into 3 different popups. With 6 checkboxes on the first one, 9 on the second one, and 6 on the last one. On every popup, only one checkbox can be selected. So 3 checkboxes can be clicked. This leaves me with 324 different variations.

Now the following thing I didn't know how to code.

I want to create 8 different if statements that trigger if certain checkboxes are checked this 8 different if statements toggle a class name, and they toggle 8 different colors.

until now I figured I have to do something like this.

`

const form = document.querySelector('form');

form.addEventListener('submit', (e) => {
    e.preventDefault();

    let allCheckboxes = {};

    document.querySelectorAll('[type="checkbox"]').forEach(item => {
        if (item.checked === true) {
            allCheckboxes[item.value] = true;
        } 
    })
    console.log(Object.entries(allCheckboxes))
})

`

this code lets me console.log out the checked checkboxes and displays them (check image).

s4, h4, e4 means the 4. checkbox from the first popup, the 4. checkbox from the second popup and the 4. chekcbox from the third popup. (these are te ones I clicked to show an example)

I would like to access these values something like this:

`

const bluecolor = document.getElementsByClassName("main");

if ("s4" && "h4" && "e4" == true || etc. ) {
bluecolor.classList.toggle("blue")
}

`

I know how to toggle classLists etc that's not a problem the problem is I don't know how to access the values in the image and use them in a if statement as condition.

I tried to access them in multiple ways but with no success.

  • `if ("s4" && "h4" && "e4" == true || etc.)` makes no sense; see [Check variable equality against a list of values](/q/4728144/4642212); use [`some`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/some) or [`every`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/every) or [`includes`](//developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/includes). `bluecolor.classList` doesn’t exist; see [What do querySelectorAll and getElementsBy\* methods return?](/q/10693845/4642212). – Sebastian Simon Nov 22 '22 at 07:16
  • `if (item.checked === true)` can be replaced by `if (item.checked)`. – Sebastian Simon Nov 22 '22 at 07:17
  • I would recommend to use react redux and store the selected values in store. this way it would be much simpler to get only the 3 selected values and check them - maybe with switch-case, or store them in an array and use some array methods. – Rachel Nov 22 '22 at 07:21
  • @Rachel unfortunately I'm a bloody beginner so I don't know how to use react-redux, could you perhaps show an example? I'm a beginner I learn quickly. – Noah Onyejese Nov 22 '22 at 07:29
  • this is [react redux](https://react-redux.js.org/), but the best solution for you will be using [redux form](https://redux-form.com/8.3.0/) - this is quit complicated topic but it is essential for things like you are facing here. I recommend to start with redux and then move to forms and then it will be much easier to you to figure this out – Rachel Nov 22 '22 at 07:47
  • 1
    @NoahOnyejese Honestly, you don’t _need_ React Redux. Do these checkboxes look like ``, ``, ``? Do you have a single element with `class="main"`? Then you’ll just need one statement: ``document.querySelector(".main").classList.toggle("blue", [ "s4", "h4", "e4" ].every((name) => document.querySelector(`[name='${name}']`).checked) ||``…`);`, etc. Getting into these Frameworks without understanding what their purpose is is often unwise. – Sebastian Simon Nov 22 '22 at 07:54
  • 1
    @Rachel - The question is not tagged React and suggesting a React solution is not helpful. – Yogi Nov 22 '22 at 08:05
  • @SebastianSimon yes that makes sense. my checkboxes look like following: // I will delete the ID I don't need it anymore // class is for the CSS // value is for the value that I'm currently working on // name is that only one can be checked // onclick is also so that only one can be checked out of (6) and the uncheck1 is for my querySelector to uncheck all checkboxes if clicking a previous button. – Noah Onyejese Nov 22 '22 at 08:34
  • @NoahOnyejese [Edit] your post. – Sebastian Simon Nov 22 '22 at 08:36
  • @SebastianSimon can I do what you did instead of name using value? – Noah Onyejese Nov 22 '22 at 08:37
  • You mean for ``? Of course. `[name='s4']` is an [attribute selector](//developer.mozilla.org/en/docs/Web/CSS/Attribute_selectors) where `name` is the name of the attribute and `'s4'` is the value of the attribute. Just replace `[name=` by `[value=`. – Sebastian Simon Nov 22 '22 at 08:43
  • @SebastianSimon currently, I get the following error: Uncaught TypeError: Cannot read properties of null (reading 'classList') – Noah Onyejese Nov 22 '22 at 08:45
  • @SebastianSimon I eliminated the error. But it's not quite working. – Noah Onyejese Nov 22 '22 at 08:50
  • @NoahOnyejese Well, you haven’t shared your HTML, but you did use `document.getElementsByClassName("main")`. That’s what an element with `class="main"` would be matched by. `document.querySelector(".main")` would match it, too. Is your ` – Sebastian Simon Nov 22 '22 at 08:51
  • @SebastianSimon this is how I have it. ` ` – Noah Onyejese Nov 22 '22 at 08:53
  • I've created a [JSFiddle](https://jsfiddle.net/xudny4cf/) showing how you can generate a code based on the number of check boxes selected in each group. – Yogi Nov 22 '22 at 09:20
  • @Yogi damn beautiful but not 100 % what I need very close tho. I would like to only be able to click 1 check box per group and give each checkbox a different vlaue (e.g. s1 s2 s3 s4 s5 s6 | h1 h2 h3 h4 h5 h6 h7 h8 h9 | e1 e2 e3 e4 e5 e6 and if s4 h4 and e4 is clicked it turns blue, is that possible? – Noah Onyejese Nov 22 '22 at 09:24
  • Yes, use radio buttons rather than check boxes. See revised [JSFiddle](https://jsfiddle.net/zm6g7cpa/1/) – Yogi Nov 22 '22 at 10:57
  • Amazing, I respect your help & work! – Noah Onyejese Nov 22 '22 at 11:09
  • @Yogi hi man I tried your code although it did work on my desktop. I tried to add the whole thing into my Shopify site, and it stopped working. I didn't get any errors but it wouldn't toggle or add anything to the class. I thought I messed up but I copied your code 1 on 1 (everything HTML CSS and js) but it still didn't work do you maybe know the reason for this? (Note I have already used add and toggle in my previous coding and it worked just fine), Thanks for your help! – Noah Onyejese Nov 23 '22 at 19:45

0 Answers0