views:

66

answers:

4

THis is what I want.

onclick="pager(function2();)

but it doesn't seem to work.

+3  A: 

Take out the semicolon:

onclick="pager(function2())"

http://jsfiddle.net/BQYVV/

You are not passing a function to a function, but just a value. When the element is clicked, function2() is called, and it's return value becomes an argument to the pager() function. If function2 does not return anything, pager will receive undefined as an argument.

Anurag
Yeah seems like this silly mistake to me :)
Charles Ma
seems like adding an extra semi-column is not always a good thing :) http://stackoverflow.com/search?q=javascript+extra+semicolon
Anurag
A: 

This works for me

<html>
<head>
</head>
<body>
<a href="#" onclick="function2(function1());">Click me</a>
<script>
    function function2(msg) { alert(msg); };
    function function1() { return "Hello World"; };
</script>
</body>
</html>

What's not working in your code?

Ben
A: 

you can do something like this to make it work

<p onclick="pager(function2())">Click Me</p>


​function pager(fn)​{
    return fn;
}
  function function2(){
    alert('hello');
  }
Reigel
A: 

If you're using jQuery, please don't use in-line script, assign the click handler, like this:

$("div").click(function() {
  pager(function2());
});

This is if you wanted to assign it to all <div> elements, if you wanted it on a particular element via ID, like this: <a href="#" id="bob">Link</a>. You would use $("#bob") instead of $("div"). There's a full list of selectors here, if you're familiar with CSS, you'll feel right at home.

Nick Craver
Yeah this is exactly what I ended up using. thanks for the help.
creocare