How to assign a attribute value to a variable? [duplicate] - javascript

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');

Related

Using a variable inside option name in Javascript [duplicate]

This question already has answers here:
Is it possible to add dynamically named properties to JavaScript object?
(20 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 1 year ago.
I need to do this:
let parallaxBp = 768;
ScrollTrigger.matchMedia({
"(min-width: " + parallaxBp + "px)": function() {
// code
}
});
but doesn't work, it says
, expected
So I can't concatenate that string. How can do it?

Get variable by variable [duplicate]

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"

Reference to variable versus "actual" variable (PHP vs Javascript) [duplicate]

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?

JS read object from a string [duplicate]

This question already has answers here:
Add a property to a JavaScript object using a variable as the name? [duplicate]
(14 answers)
Closed 4 years ago.
CODE:
let name = 'bam';
doc.name = req.body[x].value;
PROBLEM:
I want it to return doc.bam value but actually it get doc.name value while I set name='bam'.
For computed properties you should use []
var doc = {};
let name = 'bam';
doc[name] = "abc";
console.log(doc.bam);

How insert variable in JS [duplicate]

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;
}

Categories

Resources