0

I am trying to save a string variable and then use this variable as an other variable.

I am saving a string in the "name" variable that can be "weekly", "monthly" or "quarterly".

and then use is as a "this.weekly" but using the "name" reference. Just for not going through the "swtich" for each case.

This is my code:

    // Update "report" variable onclick checkbox
    checkboxSaveOnChange: function(newCheckbox){

        var name = newCheckbox.checked;


        if (newCheckbox.checked) {
            this.name.push(newCheckbox.value);  
        } else {
            var index = this.name.indexOf('newCheckbox.value'); 
            this.name.splice(index, 1); 
        }

        /*switch (name) {

            case 'weekly': 


            break;

            case 'monthly': 

            break; 

            case 'quarterly': 

            break;

        }*/


        console.log(this.weekly);
        console.log(this.monthly);
        console.log(this.quarterly);
    },

tried using var name = 'this.+'newCheckbox.checked; - doesn't work...

EDIT:

This is still resulting the error:

    // (newCheckbox.checked) ? this.name.push(newCheckbox.value) : this.name.indexOf('newCheckbox.value');
    if (newCheckbox.checked) {
        this[name].push(newCheckbox.value);     
    } else {
        var index = this[name].indexOf('newCheckbox.value'); 
        this[name].splice(index, 1);    
    }
Imnotapotato
  • 5,308
  • 13
  • 80
  • 147
  • 5
    It's `this[name]` – Pointy Jan 18 '18 at 14:51
  • Can you submit an example? I changed `var index = this[name].indexOf('newCheckbox.value');` and `this[name].splice(index, 1);` and still get the error `TypeError: this.name is undefined` – Imnotapotato Jan 18 '18 at 14:54
  • Are you still console logging `this.name`? or haven't changed `this[name].push`? – Albzi Jan 18 '18 at 14:54
  • Note that the value of the `checked` attribute of a DOM checkbox element will always be either boolean `true` or `false`, so your `name` variable will be one of those. Until you've initialized `this[name]` it will be `undefined`, so your code will fail for that reason. – Pointy Jan 18 '18 at 14:57
  • yes, edited my post at the bottom with the current code lines – Imnotapotato Jan 18 '18 at 14:57
  • @Pointy this was my problem! `name = newCheckbox.checked` should be `name = newCheckbox.name`... working for toooo many hours :( – Imnotapotato Jan 18 '18 at 15:01

2 Answers2

3

Try using this[name]. In JavaScript property accessors can work with dot notation as well as bracket notation. You can see the docs from Mozilla Developer Network MDN for details.

smtaha512
  • 420
  • 5
  • 11
1

Here's an example to elaborate on the previous answer.

var thingy = {
  weekly: false,
  monthly: false,
  quarterly: false,
  checkboxSaveOnChange: function(newCheckbox) {
    var value = newCheckbox.value,
        checked = newCheckbox.checked;
    this[value] = checked;
  }
};

document.getElementsByTagName("button")[0].onclick = function(){
  var cbs = document.querySelectorAll("input[type='checkbox']");
  for(var i=0; i<cbs.length; i++) thingy.checkboxSaveOnChange(cbs[i]);
  console.log(thingy);
};
<label><input type=checkbox value=weekly />weekly</label>
<label><input type=checkbox value=monthly />monthly</label>
<label><input type=checkbox value=quarterly />quarterly</label>
<button>do a thing</button>
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116