Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a button and I have function which should execute on click.
example:
<button id="button1">bla bla</button>
<script>
var justFunctionName = "function1";
var function1 = function()
{
alert("!");
}
$("#button1").click(justFunctionName);
</script>
HTML
<button id="button1">bla bla</button>
jQuery
var justFunctionName = "function1";
function function1()
{
alert("!");
}
$("#button1").on("click", window[justFunctionName]);
See working jsFiddle demo
$("#button1").click(justFunctionName);
should be
$("#button1").click(function1);
var justFunctionName = "function1";
This is assigning the string function1 to the variable justFunctionName.
If you were to do: console.log('justFunctionName'); you would end up with the following result:
>function1
Therefore this variable assignment is completely irrelevant for what you are hoping to achieve. Instead of just assigning a variable to a function you are assigning a variable to a variable which is assigned to a function.
Now take a look at what you are doing here:
var function1 = function () {
alert("!");
};
This is assigning a variable function1 to the function doing the alert. In this instance, think of the variable as a reference to the function. In order to have the button trigger the alert, you need to call the reference to the function (in this case function1):
$("#button1").click(function1);
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
In Javascript...If a variable is defined globally and I use a function to manipulate the value of that variable - do I have to return at the end of the function or does it matter since the variable is global?
I just experimented with the situation you describe and it appears that you do not need to return the global variable from the function in order for the variable to be altered by the function. Please see the code and run it.
var myVar = 'x';
function myFunction(){
myVar = 'y';
}
myFunction();
console.log(myVar);
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 10 months ago.
Improve this question
I just have a super quick question about JavaScript so I'm not going to follow the general formatting of a question like usual.
I'm trying to understand some code in JavaScript, and the person who wrote it used a method that I'm not familiar with. As you see in the title, they've written it by typing the name of the function followed by a colon first, and then writing the actual function.
Example: myFunction: function () {}
Is anyone able to tell me what this means, why it's done, and what it accomplishes?
It is part of Object Methods like:
const person = {
myFunction: function() {}
};
you can invoke myfunction by using person.myFunction()
There are 3 ways of writing a function in JavaScript:
function myFunction() {} // Function Declaration
myFunction: function () {} // Function Expression
myFunction = () => {} // Arrow Function
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Suppose, I have one global variable 'x' and I want to set it to value '2' i.e. x=2 inside $(document).ready(function(){}) function.
Now I want to use the variable 'x' having value '2' outside $(document).ready(function(){}) function.
Note that the usage of variable 'x' after $(document).ready(function(){}) should be independent i.e. it should not be used inside some function or callback function.
Is it possible to do so? If yes how? If no why?
Please provide me detailed solution with proper reasons for this.
Thanks.
Note that the usage of variable 'x' after $(document).ready(function(){}) should be independent i.e. it should not be used inside some function or callback function.
If it isn't inside another function, then you will be trying to use it immediately … which is before the ready event fires … so it will be before the value has been assigned to the variable.
In short: No.
You will have to declare a global variable x before the function.
Like:
var x;
$(document).ready(function(){ x = 2; }); console.log(x);
In this case you are defining a global variable and assigning a value to that variable under a function. So you can access that x outside that function as well.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have a JavaScript function like this:
obj.use('hello', function () {
// Here I want to access the string 'hello'
});
Is this at all possible, or are there any good workarounds that I can use?
My answer is based on the assumption that your usage indicates use as being a method of the object obj. In which case the way to achieve your objective is to use the same object to store the first parameter to obj.use.
var obj = {
use: function(greeting, fn) {
obj.greeting = greeting;
fn();
}
}
obj.use('hello', function () {
console.log(obj.greeting); //output: 'hello'
});
// Here I want to access the string 'hello'
Well that's easy, you just store the string in a variable whose scope is accessible inside the method:
var greeting = 'hello';
obj.use(greeting, function () {
console.log(greeting); // logs "hello"
});
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I've the following code:
function hi(){
alert("hi");
}
window["hi"]();
I thought the hi will be executed. Is there anything similar, that I could do? (no eval please)
Edit:
I want it in bracket notation as the function names will be in string.
If it doesn't work, you're probably in the wrong scope, try
window.hi = function(){
alert("hi");
}
window["hi"]();
EDIT:
If testing in jsFiddle, the default option of onLoad, or the onDomReady option inserts an event handler that waits for DOM ready with a callback.
The callback function creates a new scope, so when adding properties to the window object you have to reference it specifically with window.prop, just typing prop is not enough as it's not in the right scope.
You can just call it directly:
hi();
Or
window.hi();
if you really want to involve the global object.
edit if you want to access the function by a dynamic name, then you certainly can, but the function must be accessible as a property of some object. The global object is one possibility:
function hi() {
alert("hi");
}
window.hi = hi;
Then window["hi"]() will work. Alternatively, you can create an object for the purpose:
var functionRegistry = {}
function hi() {
alert("hi");
}
functionRegistry.hi = hi;
Then functionRegistry["hi"]() will work.