#Callback function inside another function

14 messages · Page 1 of 1 (latest)

modern portal
#

Hi! I'm having troubles with combining functions in multistep form.
I have two steps inside my form.

on Step 1 - (workspaces) there are a few inputs as checkboxes for user to select from.
Step 2 - (brands) shows another set of input checkboxes, based on selection from step 1.

I have written below code, to check that:
``javascript
function checkWorkspaceAccess(){
const workspaceInputs = document.querySelectorAll('.access-input__workspace');
const brandInputs = document.querySelectorAll('.access-input__brand');

workspaceInputs.forEach((workspaceInput) => {
workspaceInput.addEventListener('input', () => {
brandInputs.forEach((brandInput) => {
if (workspaceInput.checked){
brandInput.disabled = false;
} else{
brandInput.disabled = true;
}
})
})
})
} ```
But on Step 1, I also have button with 'select-all' inputs functionality. The problem is, When I use this button, all checkboxes from step 1 change their 'checked' value, but inputs from Step 2 are not affected.

``javascript
function selectAllWorkspaces(){
const selectAllBtnWorkspaces = document.querySelector('.form-select-all__workspaces');
const workspaceInputs = document.querySelectorAll('.access-input__workspace');

selectAllBtnWorkspaces.addEventListener('click', ()=> {
workspaceInputs.forEach(workspaceInput => {
workspaceInput.checked = !workspaceInput.checked;
})
})
}

I have tried to put one function as an argument to another, or just simply invoke it inside - but  it didn't work. 
How can I combine these two functions to work?
twin silo
#

Do you have a link to a workspace I can check out? So I can see the full code?

modern portal
#

At this moment I can only share screenshots 😦 it's a part of a bigger project, Vanilla JS and PHP (html as Twig)

twin silo
#

Is there a reason you aren't defining the variables outside the function? i.e selectAllButtonWorkspaces and workspaceInputs?

#

Also it might make sense to have your function just make ALL the inputs checked, and unchecked if they're already all checked. I think the way you've done it would just flip each one from off-on or vice versa?

modern portal
#

No particular reason, I defined them outside of functions, but then as I was struggling to make it all work I started to define them inside functions.. now after I look at it it might be not the best thing to do 🥲

#

Yes, it turns them off-on... I will change logic to check if they are not checked and only check them then

twin silo
#

It's hard to see from here, without the code in front of me, but you'd generally want a function to run to GIVE all of these elements their event listeners

#

and give each one a named function onclick

#

then a separate function, which is the named one, which does all the logic checkin

#

I think that's your issue, you're trying to do both things in the same function

modern portal
#

ok, I will try to separate them and see if that helps

twin silo
#

And if you see yourself repeating your code, like the queryselectors you've done the same a few times, think about doing it once 'outside' the function, then just referencing it in each function you need it in

modern portal
#

oh ok! Thank you for your tips ❤️