0

I'am trying to migrate from jquery 1.7 to 1.10 and the live function do not work anymore.

$("#detail_content").on("click", ".close", function (a) {  // is ignored
//$("#detail_content .close").live("click", function (a) { //works fine with migrate
  console.log("click");
});

the div.detail_content is loading later via ajax but the close button do not work anymore if i change from .live to .on

I think the delegation is missing.

any idea?

hamburger
  • 1,339
  • 4
  • 20
  • 41
  • possible duplicate of [jQuery: how to replace .live with .on?](http://stackoverflow.com/questions/10604328/jquery-how-to-replace-live-with-on) – VisioN Jun 06 '13 at 11:46
  • it is not duplicated. the syntax is ok. In this case is the missing static parent the subject. – hamburger Jun 06 '13 at 12:08

4 Answers4

4

Looks like #detail_content also is an dynamic one, then try

$(document).on("click", "#detail_content .close", function (a) {  // is ignored
//$("#detail_content .close").live("click", function (a) { //works fine with migrate
  console.log("click");
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
2

You should use any closest static parent element (or body at last):

$("body").on("click", "#detail_content .close", function() { ... });

So if you have the markup like:

<body>
    ...
    <div id="container">
        ...
        <div id="detail_content"><button class="close">Close</button></div>
    </div>
</body>

and #container is not replaced after Ajax call, then it is better to use:

$("#container").on("click", "#detail_content .close", function() { ... });
VisioN
  • 143,310
  • 32
  • 282
  • 281
1

The .live() method is deprecated in jQuery 1.10 and above. Use .on() method to attach event handlers.

So you can use this code instead of .live()

 $(document).on('click', '#detail_content .close', function(){
      //your Code
 });

I think this answer is useful for you and in this page you can see all deprecated method and it's useful for someone who want to migration from 1.7 to 1.10

Community
  • 1
  • 1
Majid Golshadi
  • 2,686
  • 2
  • 20
  • 29
0

VisioN, is it not the same to just use the following?

$(document).ready(function(){
    $(".close").click(function(){
        console.log("clicked");
    }
});

Is there something about the code above that is slower or less efficient somehow?

Sean Kendle
  • 3,538
  • 1
  • 27
  • 34