Coolguy Coolguy - 7 days ago 5
CSS Question

Javascript bootstrap modal calling function

My code is like below:

function OnApply(runFunc) {
$("#myCfmModal").modal('show');
$("#myCfmModal").find("#myCfmModalText").html("Apply change now?");
$("#myCfmModal").find("#myCfmModalHeader").css("background","#cccccc");
$("#myCfmModal").one('click', '#okbtn', runFunc);
}

function cool() {
alert("cool!");
}


I have a problem running the code above. I call the
OnApply(cool())
as below:

<button class="btn btn-success" onclick="OnApply(cool())">Apply</button>


OnApply(cool())
will show a modal box and run a function
cool()
. What I want to do is to run function
cool()
when I click the OK button in the modal box. However the
cool()
function will always run first before I click the OK button. Any idea what went wrong?

Answer

The issue is because you are passing the result of the cool() function to OnApply() - you instead need to pass the reference to the function by removing the trailing brackets. Try this:

<button class="btn btn-success" onclick="OnApply(cool)">Apply</button>
Comments