views:

181

answers:

7

I was trying the next code without success

HTML

<a id="addBookButton" href="javascript:showForm('addBookButton','add-book','activateAddBookForm');" class="addA"><span>Add Book</span></a>

Javascript


function showForm(button,form,callback) {
    $("#"+button).hide();
        $("#"+form).show();
        callback();
}
+2  A: 

Try this:

function showForm(button,form,callback) {
    $("#"+button).hide();
    $("#"+form).show();
    if (typeof this[callback] == "function") this[callback]();
}

Of you pass the function by value and not just the name of it:

<a id="addBookButton" href="javascript:showForm('addBookButton','add-book',activateAddBookForm);" class="addA"><span>Add Book</span></a>
Gumbo
A: 

Change your a tag to:

<a id="addBookButton" href="#" onclick="showForm('addBookButton','add-book','activateAddBookForm'); return false;" class="addA"><span>Add Book</span></a>

Note the onclick handler, and the return false; within the onclick.

Hooray Im Helping
+5  A: 

You can just pass a reference to the function into your showForm function.

<a id="addBookButton" href="javascript:showForm('addBookButton','add-book',activateAddBookForm);" class="addA"><span>Add Book</span></a>
AgileJon
A: 
function showForm(button,form,callbackName) {
    $("#"+button).hide();
    $("#"+form).show();
    var callback = window.callbackName;
    if(typeof callback === 'function') {
        callback();
    }
}
PatrikAkerstrand
A: 

Whoops totally wrong answer :)

Ropstah
A: 

One option is to pass the function be reference instead of as a string, so:

... javascript:showForm('addBookButton','add-book', activateAddBookForm); ....

or to continue using the string:

function showForm(button,form,callback) {
    $("#"+button).hide();
        $("#"+form).show();
        new Function(callback + '();')();
}
Sean Bright
A: 
<a id="addBookButton" onclick="javascript:showForm('addBookButton','add-book','activateAddBookForm');" class="addA"><span>Add Book</span></a>

Javascript

function showForm(button,form,callback) {
  // Your Code
}

function callFunction(){
  document.getElementById('addBookButton').onclick();
}