0

I have a question that I could not find the answer, or perhaps cannot phrase the way it should...

I would like to trick javascript's way of handling variables...

Let's say in php I could do something like:

$test['usr_'.$id]=826

But when I try to do the same in Javascript/jQuery:

$("#usr_rank_h").val('rank_'+id);

It will output rank_826 instead of the value of the var rank_826

Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
hexYeah
  • 1,040
  • 2
  • 14
  • 24

1 Answers1

1

The equivalent idiom in javascript is actually

var id = 826;

var test = {};
test['rank_'+id] = 826;

Which gives you back an object of the form

{
   'rank_826': 826
}

PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?

Damien Fayol
  • 958
  • 7
  • 17
  • I am passing data from php to an input... I am slowly getting into jQuery ... It might not be the best way for this instance but it works now and I was wanted to know anyway ! thanks !! – hexYeah Feb 17 '16 at 20:11