0

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();
}

6 Answers 6

5

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>
2

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>
0

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.

0
function showForm(button,form,callbackName) {
    $("#"+button).hide();
    $("#"+form).show();
    var callback = window.callbackName;
    if(typeof callback === 'function') {
        callback();
    }
}
0
<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();
}
-1

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 + '();')();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.