-1

I use loadData() function to get data from my php script and I use another function to search from database. All works fine, but when I click on the search result, it's not loading the data. Here is the jquery functions I am using,

//Gets Data
function loadData(id)
{
loading_show();
$.ajax({
      url: "load_data.php",
      type: "post",
      data: "id="+id,
      success: function(data){
            loading_hide();
           $("#container_wrap_metro").html(data);
      },
      error:function(){
          alert("failure");
          $("#container_wrap_metro").html('Unable to process request!');
      }  
    }); 
}


//Search
$(function(){
  $("#result").keyup(function(){
    var q = $(this).val();
    $.get("results.php?q="+q, function(data){
    if(q){
        $(".side_container").html(data);
    } 
    else {
        $(".side_container").html("");
    }
  });
});
$('#b').live('click',function(){
  var page = $(this).attr('p');
  loadData(page);
  }); 
});
6
  • And what is $('#b')???
    – A. Wolff
    May 3, 2013 at 11:02
  • check the jQuery version as .live() is Deprecated in version> 1.7
    – Ishank
    May 3, 2013 at 11:03
  • So your problem is with live(), read doc, all is explain...
    – A. Wolff
    May 3, 2013 at 11:06
  • @roasted 'b' is the id of the div (which holds the search result) May 3, 2013 at 11:11
  • You should have post relevant html code. Which elements (#b,#result) if any are dynamically added???
    – A. Wolff
    May 3, 2013 at 11:13

5 Answers 5

3

The answer is:

$(document).on('click','#b',function(){...}); //live equivalent

But better is to use closest static container:

$('#container_wrap_metro').on('click','#b',function(){...});
0
3

.live() is Deprecated so you can try .on() instead of .live().

http://api.jquery.com/live/ & http://api.jquery.com/on/

0

Which jQuery version are you using? the live function may be deprecated. Use .on() instead or .click() directly?

8
  • I use jquery 1.9.1 , Can you please suggest a way to write using .on() function, i am new to jquery May 3, 2013 at 11:03
  • just replace "live" with "on" :) $("#b").on("click", function(){...});
    – Alex
    May 3, 2013 at 11:04
  • No, you HAVE TO USE DELEGATION! Sorry for screaming
    – A. Wolff
    May 3, 2013 at 11:05
  • @roasted are you sure we have to? I am pretty sure it works without delegation, too. Else please explain than just shout
    – Alex
    May 3, 2013 at 11:08
  • Ya, for dynamic element, you have to use delegation
    – A. Wolff
    May 3, 2013 at 11:09
0

Use On() as live() is deprecated.

$(document).on("click", "a.foo", fn);  //here a.foo is target

http://jquery.com/upgrade-guide/1.9/#live-removed

1
  • No, you are not using delegation here! BTW, there is a typo
    – A. Wolff
    May 3, 2013 at 11:03
0

I have replaced live with on but it doesn't work for me. You can use .delegate()

In my case, I have written a click in the dynamically loaded content. When I updated the JQuery Library, the live stopped working & replaced it with delegate today.

See more information here: https://api.jquery.com/delegate/

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.