This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
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
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 ?
Related
This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
Closed 2 years ago.
I have looked all over google and i have found nothing that fits my needs. How would you add an item to an object. I basically have a string variable that i want as the key. Therefore i cant use obj.key = 'something';. I sort of want it like:
obj = {'somekey': 'somevalue'};
obj.add('someotherkey': 'someothervalue');
console.log(obj);
by obj.add() i mean someone's solution
and then console says {'somekey': 'somevalue', 'someotherkey': 'someothervalue'}
Does anyone know a way to do this. I don't care at what position it is just whether it is there. By the way i'm using node if that helps. If you have any questions on my code please ask.
Very simple:
obj["somekey"] = "somevalue";
You can also use a variable instead:
let myVariable = "somekey";
obj[myVariable] = "somevalue";
Or you just use normal object notation:
obj.somekey = "somevalue"
This question already has answers here:
Why does jQuery or a DOM method such as getElementById not find the element?
(6 answers)
Closed 2 years ago.
I have the following problem to solve, when I start writing text in input, this one should turn red. Unfortunately, for unknown reasons, the variable is unidentified. Perhaps the problem is that js is not able to define the variable as input. I don't understand something here. Please help.
var input_Login = document.getElementsByTagName('input')[0];
console.log(input_Login);
function inputCheck(input_Login){
input_Login.style.color='pink';
}
var doit = input_Login.addEventListener('oninput', inputCheck);
You need to use the event input and the keyword thisin your function, see the example below:
var input_Login = document.getElementsByTagName('input')[0];
console.log(input_Login);
function inputCheck(input_Login){
this.style.color='pink';
}
var doit = input_Login.addEventListener('input', inputCheck);
<input type="text">
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 9 years ago.
I can't figure out how to use the name of a variable previously created with eval, without knowing it.
I mean:
function getName(menu_name, level){
eval("var menu_"+level+"="+menu_name);
}
Now how do I get the name of the variable I just created? Probably keep using eval, but I have to put that name into a $.post call as one of my field name.
Thanks in advice.
If level is an integer, you can treat it as a numerical index for an array:
var menu = [];
menu[level] = menu_name;
If level is anything else, you can treat it as a key for a dictionary/associative array:
var menu = {};
menu[level] = menu_name;
Then, for either of the solutions, if you want to access your menu_name, simply call menu[level].
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Dynamic object property name
I have a function that I need to call based on a user inputted data.
So for example, I have:
models.cat
models.dog
Now, I want to be able to call models.[my_str] where my_str = "snake". So the computer would think it is trying to execute models.snake. Is there a way to do this in javascript or better yet coffeescript?
You should be able to call it like so:
models[my_str]();
This should work in both Javascript and Coffeescript.
This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Javascript Objects: Dynamic variable names?
I have a json string passed in from a cgi script. This json string has a list of id's. In javascript
var informationObj = jQuery.parseJSON(information);
tid = informationObj.idList[0].id;
tid is now an ID and I want to use it to access objects within the json string itself like so:
alert (informationObj.tid.rpath);
However this does not seem to work. I have also tried:
alert (informationObj.eval(tid).rpath);
Is there a way around this?
Thanks.
You need this form:
informationObj[tid].rpath
They are equivalent:
var a = 'something';
b[a] === b.something
Use bracket notation:
alert(informationObj[tid].rpath);