0

I have a need to pass the variable value as a name in the below function using [tokenName] for CSRF purposes. It works fine in Firefox and Chrome, but not IE11.

The error I get is: Expected identifier, string or number on the [tokenName] line.

Thoughts?

    $('#container').on('click','#delete',function(e) {
        e.preventDefault();
        var button = $(this),
            form = button.closest('form'),
            id = button.attr('data-id'),
            verify = button.attr('data-verify'),
            tokenName = form.find('input[name=token_name]').val(),
            tokenHash = form.find('input[name=' + tokenName + ']').val();

        // delete user via ajax
        $.post(form.attr('action'), {
                "id" : id,
                "object" : verify,
                [tokenName] : tokenHash
            }, function(data) {

            // do stuff


        }, "json");

    });
Dario Zadro
  • 1,143
  • 2
  • 13
  • 23

2 Answers2

3

You can always fall back to older syntax:

  var options = {
      "id" : id,
      "object" : verify,
  };

  options[tokenName] = tokenHash;

  $.post(form.attr('action'), options, function(data) {
      //...
  });
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

Using variable as a key in a Javascript object literal is an ES6 Feature

[tokenName] : tokenHash this feature is not supported by IE 11

Click here to see ES Compatability Tables for various js engines

S. Patel
  • 182
  • 1
  • 10