3

My soft.php code

<!DOCTYPE html>
<html>
<head><style>
</style>
<script src="jquery.js"></script>
<script>
$(function(){
$("button").on("click",function(){alert("f");});
$("div").on("click",function(){
$("span").load("check.php");
});

});</script></head><body>
<div>Click</div>
<button>eaf</button><span></span>
</body></html>

And check.php code

<body>
<button>Hello</button>
</body>

I want to alert f on clicking on button which is loaded via ajax which was working fine with .live(). But someone told me that .on() can be used in place of live but .on() is not working here.Suggest Solution

  • 1
    `.on()` requires jQuery v1.7 or above. Which version are you using? – andyb Jun 06 '12 at 11:25
  • the one which is stated by you –  Jun 06 '12 at 11:25
  • You did not properly convert your `.live` code to `.on`. **[Please read the documentation](http://api.jquery.com/live/)** and/or **[use the search](http://stackoverflow.com/questions/8021436/jquery-1-7-turning-live-into-on/8021462#)**. – Felix Kling Jun 06 '12 at 11:28

1 Answers1

7
$('body').on("click", "button", function(){
    alert("f");
});

As your button added to DOM after page load you need to use delegate event.

It would be better to use something other selector instead of body.

You can use multiple selector like follwoing:

$('body').on("click", "button, div", function(){
    alert("f");
});

Read about .on()

thecodeparadox
  • 86,271
  • 21
  • 138
  • 164