How to call functions dynamically in Javascript [duplicate] - javascript

This question already has answers here:
How to pass extra parameter to event handling callback?
(2 answers)
Pass an extra argument to a callback function
(5 answers)
Closed 5 years ago.
I have a function where I would like to call classList methods dynamically, something like this:
function expand(action) {
searchContainer.classList[action]("expanded");
}
button.addEventListener("click", expand('add'), false);
searchContainer.addEventListener("onblur", expand('remove'), false);
But not sure how can I do that?

Related

Javascript how i call function inside another function [duplicate]

This question already has answers here:
What is the scope of variables in JavaScript?
(27 answers)
Javascript call nested function
(11 answers)
Closed 2 months ago.
I have this function inside a function, how i call fullName function?
var person = function(){
function fullName(){}
}
person.fullName(); This doesn't work

JavaScript anonymous function syntax? [duplicate]

This question already has answers here:
Why do I have to omit parentheses when passing a function as an argument?
(2 answers)
JS: What's the difference between a ! closure and () closure? [duplicate]
(2 answers)
Closed 6 months ago.
I have a question. Why when I'm using anonymous function and I want to call it immediately i have to do like that:
(() => {console.log('something')})()
But when I want to use it with setTimeout for example, I don't have to use '(' at beginning and '()' at the end?
setTimeout(() => {console.log('something')}, 2000)
You pass the function as a parameter to setTimeout, rather than calling it right away. That's why you don't need the () at the end

I don't know how many parameters my function will accept(javascript) [duplicate]

This question already has answers here:
Is it possible to send a variable number of arguments to a JavaScript function?
(12 answers)
JavaScript variable number of arguments to function
(12 answers)
Closed 7 years ago.
I wanted to have a function that accepts multiple integer parameters and I am not sure how may parameters will be passed to the function, but I will use all the parameters passed.
It will be some think like this
function abc(.......){
// I want to transfer all the parameters passed to one array
var xyz = arguments[];
}
abc(1,2,3,4,5,.....);

How to pass id of an element to a js function? [duplicate]

This question already has answers here:
Pass element ID to Javascript function
(5 answers)
How to pass the id of an element that triggers an `onclick` event to the event handling function
(7 answers)
Closed 7 years ago.
I am using tag, and I want to pass its id to function when users clicks on it, I am doing code in onclick event.
following is my html code;
<a class="color_2" id="1" onclick="ConfirmDelete()">Delete</a>
here I want to pass this is to ConfirmDelete() function.
You can just type onclick="ConfirmDelete(this.id) and it will be sent to your function.
and in function definition;
function ConfirmDelete(var_id)
{
alert (var_id);
}

Use a variable as function in jquery [duplicate]

This question already has answers here:
Javascript dynamically invoke object method from string
(5 answers)
Closed 8 years ago.
I want to use a method in which i will pass a function_name as parameter to another function.
On the other function, the parameter will be treated as a function
here's my code example
<div class="btn_crt_acct" onclick="toggle_object('register_div','slideDown');">
CREATE AN ACCOUNT
</div>
whch will call a function like this
function toggle_object(obj,fun)
{
$('#'+obj).fun('slow');
// fun => slideDown
// so $('#'+obj).fun('slow'); => $('#'+obj).slideDown('slow');
}
but i am doing something wrong as it states an error in console, $fun(..) is not a function.
How can i make it work perfectly??
Thanks
You'd do that with bracket notation
$('#'+obj)[fun]('slow');
FIDDLE
But why not use a proper event handler, and slideToggle if you intend to toggle it
$('.btn_crt_acct').on('click', function() {
$('#register_div').slideToggle();
});

Categories