0

I am using toastr and want to set the options with a json object that is returned from ajax call. I am having some trouble setting the options property and value dynamically. Here is sample code:

if(data.toast){
    $.each(data.toast, function(index, element) {
    toastr.options.index = element;
    });
}

And data.toast is a json object that looks like this:

"toast":{"closeButton":true}

Here is what it would look like if I hardcoded it:

toastr.options.closeButton = true;

What should I change the .index in the iterator to so that it will evaluate it as a variable

itsGlen
  • 40
  • 6

1 Answers1

1
if(data.toast){
    $.each(data.toast, function(index, element) {
    toastr.options[index] = element;
    });
}

Doing it as options.index attempts to access the property of index not the property of the value of index.

The above method will fix that. It treats the object as a sort of associative array. So toastr.options[index] evaluates to toastr.options["closeButton"] which is the same as toastr.options.closeButton.

Devin H.
  • 1,065
  • 1
  • 11
  • 19