Skip to content Skip to sidebar Skip to footer

Cannot Select Id With Jquery From $.get() / $.load() Function. Please Help Me?

My Javascript something like this $('button').click(function(){ //load the data and place inside to #content }); $('#id-from-data-that-load').click(function(){ //Do some actio

Solution 1:

You need to use live for the div event instead. Here's what it should be:

$('#id-from-data-that-load').live("click", function(){
  //Do some action
});

Or alternatively you could also do this:

var submitted = false;

$('button').click(function(){
  // If the button was clicked before, we don't submit again.if (submitted == true) { returnfalse; }

  // Set submitted to true, so when user clicks the button again,// this operation will not be processed one more time.
  submitted = true;

  //load the data and place inside to #content
  $.post("/getdata", {}, function(response) {
      //after the load happened we will insert the data into the div.
      $("#content").html(response);

      // Do the bindings here.
      $('#id-from-data-that-load').click(function(){
        //Do some action
      });
  });

  returnfalse;
});

Solution 2:

try:

$('#id-from-data-that-load').live("click", function() {

                  alert($(this).attr("id");

                });

Solution 3:

you need to bind events to elements loaded at runtime through either the .live handler or .delegate handler. That is because when you are binding events to elements they are not present on the dom. So .live and .delegate would help you achieve that. See example from @shree's answer

Solution 4:

Bind id-from-data-that-load after data load in content.

  $('button').click(function(){
      //load the data and place inside to #content
      $('#id-from-data-that-load').click(function(){
        //Do some action
     });
    });

Solution 5:

When you bind the LIs you can hardcode the function name into the onclick attribute.

<lionclick="myFunction">Blah Blah Blah</li>

Post a Comment for "Cannot Select Id With Jquery From $.get() / $.load() Function. Please Help Me?"