Computed Properties to update variable in Vanilla JavaScript

Viewed 29

ComputedPropertie question

Hey everyone, i need help with something. I've divided the picture into sections so it's easier to look at. This is really bugging me and i don't know if i can solve it this way. Thanks to anyone that can help me... Here i go:

  1. So in section 1 i've created a basic input with a name property, actually i've made 4 of them but this one is the example (the name property is important)

  2. In section 2 , those are the inputs on the page and i need to change the variable depending if the inputs is checked or not

  3. The variable in JS file is called requireInteraction and it's set to false

  4. So im doing a forEach on those 4 inputs (each one has a name property that matches the variable name in JS) and i want to change the variable in JS if the checkbox with that name is Checked. I tried using Computed Properties. So when silent is checked (with the silent name property) i want the variable "silent" in JS to switch to true. How can i extract the input name (which has the same name of the JS variable) and make it so it's like i actually typed "silent = true" and changed the JS variable.

1 Answers

I don't really know why you can't use

requireInteraction = true;

but here, in case that you have some reason you can't just use the variable directly, you could use an object to serve as a target to index into:

const context = {
    requireInteraction: false,
};

Then later on, you can use something similar to your original code:

context[el.name] = true;
Related