0

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

2
  • Why not just pass the function directly? e.g. onclick="toggle_object('register_div', slideDown);"> Commented Jan 31, 2015 at 17:25
  • @FelixKling it's a dupe, but it's also an XY problem... Commented Jan 31, 2015 at 17:29

1 Answer 1

2

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();
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.