3

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?

5
  • How are you calling OnApply()? Commented Apr 14, 2015 at 9:54
  • I call it when I click a button Commented Apr 14, 2015 at 9:56
  • Ok, but do you pass the reference of the function, OnApply(cool), or the returned value of the function, OnApply(cool())? The latter will cause this issue, as you should be using the first method. Commented Apr 14, 2015 at 9:57
  • Check this plugin : github.com/jschr/bootstrap-modal .. you can stack modals in it too .. and you achieve what you are trying to do easily .. Commented Apr 14, 2015 at 9:59
  • Rory McCrossan, what do you mean by pass reference of function? Commented Apr 14, 2015 at 10:00

2 Answers 2

2

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

Comments

0

You can call runFunc using "click" JavaScript event.

Try this.

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

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.