0

Is it possible to use button attribute as a variable name of an array in JavaScript? I tried to do it as follows but it returns syntax error.

var $(this).attr("value") = [];

Is there any way to do this?

5
  • var attrvalue = $(this).attr("value") ? Commented Jan 18, 2017 at 5:11
  • 2
    use an object and define it as object property..... var obj = {}; obj[$(this).attr("value")] = []; ..... or you can initialize it as global variable window[$(this).attr("value")] = []; Commented Jan 18, 2017 at 5:11
  • var attrValue = $(element).attr("attr key"); Commented Jan 18, 2017 at 5:12
  • @guradio I want to use $(this).attr("value") as variable name. Commented Jan 18, 2017 at 5:13
  • 1
    @PranavCBalan Your answer is correct. If you post it as an answer I can accept it. Commented Jan 18, 2017 at 6:02

1 Answer 1

0
var buttonValueAttribute = $(this).attr('value');
Sign up to request clarification or add additional context in comments.

2 Comments

I want to use $(this).attr("value") as variable name.
That is not possible - you cannot use certain characters in variable names, like the .

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.