This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
jQuery : how to access the name of the element [duplicate]
(3 answers)
Closed 1 year ago.
I have 2 variables.
var a = 1;
var b = 2;
Now I have 2 input boxes with name 'form-1-a' and 'form-1-b'.
I need to put variables in these input boxes. Please look at below code as it is not working.
$("input[name^='form-']").val(eval($(this).attr('name').split('-')[2]));
Though entire code is bit complex but simplified and it does not seem to work without any error message.
Javascript is fun and pain at same time.
Below code worked.
$("input[name^='form-']").each(function(){
$(this).val(eval($(this).attr('name').split('-')[2]))
});
Related
This question already has answers here:
Rules for unquoted JavaScript Object Literal Keys?
(6 answers)
What characters are valid for JavaScript variable names?
(12 answers)
Closed 1 year ago.
I am seeing it crop up more and more in code I am going through on a new project (can't share due to contractual reasons) where Ill see something like:
{
prop1: value$ref,
$prop2: null
}
I have see ${prop3} before, but never an example without the brackets. Can anyone provide direction as to what the method is, or the operator is or whatever the case?
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 1 year ago.
let varone = "vartwo";
let vartwo = "answer";
And I would like to get "answer". But I cannot do the following:
console.log(vartwo);
How can I get "answer", but not by including "vartwo" in my code?
I think one way to resolve it, is by using javascript objects, as shown:
obj={
varone:"vartwo",
vartwo:"answer"
};
console.log(obj[varone]) //outputs "answer"
This question already has answers here:
PHP foreach change original array values [duplicate]
(5 answers)
PHP Pass by reference in foreach [duplicate]
(9 answers)
Closed 4 years ago.
In the Javascript code:
var people = [{name:"john",age:20},{name:"bob",age:30},{name:"kate",age:40}];
people.forEach(function(person,i){
person.isHuman = true;
})
console.log(people) would give
[{name:"john",age:20,isHuman:true},{name:"bob",age:30,isHuman:true},{name:"kate",age:40,isHuman:true}]
However, the same code in PHP
$people = [["name"=>"john","age"=>20],["name"=>"bob","age"=>30],["name"=>"kate","age"=>40]];
foreach($people as $i=>$person){
$person['isHuman']=true;
}
var_dump($people) would give
[["name"=>"john","age"=>20],["name"=>"bob","age"=>30],["name"=>"kate","age"=>40]]
The new "isHuman" property is not added to the original array of objects.
In the PHP case, I know you can do "$people[$i]['isHuman']=true" to alter the object in the original array. But is there a way to do "$person['isHuman']=true" and have that change reflected in the original array?
And my second question is why/for what reasons is this behavior different between Javascript and PHP?
This question already has answers here:
JavaScript Dynamic Variable Names [duplicate]
(6 answers)
Closed 6 years ago.
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?
var buttonValueAttribute = $(this).attr('value');
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 9 years ago.
I need help, how to insert variable n as XX. Variable n is number, for example 3 and I need to get this number to document.form1.any3.value; is it possible? Thanks for advice
function pokus(n){
var any1 = document.form1.anyXX.value;
}
Use square bracket notation:
function pokus(n){
var any1 = document.form1['any'+n].value;
}