Refer to passed in variable in JavaScript function [duplicate] - javascript

This question already has answers here:
Accessing variables indirectly [duplicate]
(4 answers)
Closed 6 years ago.
<script type="text/javascript">
function parent_work_copy(track_number) {
document.form1.track_(track_number)_name.value=document.form1.track_(track_number)_parent_work.value;
}
</script>
If the link below is clicked, I want the line inside of the parent_work_copy function to be modified to:
document.form1.track_5_name.value=document.form1.track_5_parent_work.value
copy to track
How to do this?

Javascript object keys works as string
document.form1["track_"+track_number+"_name"].value=document.form1["track_"+track_number+"_parent_work"].value;

Do something like this :
document.form1['track_'+track_number+'_name'].value

Related

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?

What (jQuery) means in the end of function? [duplicate]

This question already has answers here:
What is the (function() { } )() construct in JavaScript?
(28 answers)
What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
(10 answers)
Closed 7 years ago.
I have a function:
var waitingDialog = (function($){
.....
return{
}
})(jQuery);
Also could you explain what $ means in the function? Is it going to work without that?
It means that jQuery (if it exists) will be passed to the function. $ is merely the name that variable will take in the scope of the function.
There is a section about that in official jQuery website : https://learn.jquery.com/plugins/basic-plugin-creation/#protecting-the-alias-and-adding-scope

Javascript - what is the purpose of wrapping functions or codes in a function? [duplicate]

This question already has answers here:
What is the purpose of a self executing function in javascript?
(21 answers)
What is the (function() { } )() construct in JavaScript?
(28 answers)
Closed 8 years ago.
This probably is not a new question, but where is the purpose of wrapping a function or codes inside ((function () {...})());? for instance,
//Self-evoking anonymous functions
((function () {
alert("hi");
})());
What's the difference with no wrap,
alert("hi");
I still get the same result - hi
What can you pass/ put in the brackets in the end bit - })());? and why?
Using a function creates a scope. You can have params inside and do more than just alerting.
Now you can do the same without a function, but then you will keep the state on the window object and thats something that you would like to prevent in some cases.

Make a javascript function be called when the value after hash changes [duplicate]

This question already has answers here:
How can I detect changes in location hash?
(11 answers)
Closed 9 years ago.
How do I call a javascript function every time the URL has a change with the string after the hash?
for example
.com/onepage/
(onwindowload called)
.com/#1000
(need a function to be called)
.com/#500
(need a function to be called)
Here is a way......
window.location.watch(
'hash',
function(id,oldVal,newVal){
console.log(oldval+" to "+newVal);
//Do something.........
}
);

Javascript-How to implement target=_blank in a simple link [duplicate]

This question already has answers here:
JavaScript: location.href to open in new window/tab?
(7 answers)
How to add/update an attribute to an HTML element using JavaScript?
(4 answers)
Closed 9 years ago.
So, this is my code:
function link1()
{
window.location.href="Example";
}
How do i achieve the _blank function in JS?
Cheers
(Note: I am not using a library, nor am i intending to atm)
window.open will do the trick.
function link1()
{
window.open("http://www.example.com");
}

Categories

Resources