views:

324

answers:

1

Hello,

I have a link that calls the ajax to load content, and after the content is loaded, my jquery function doesn't work anymore

Here is my HTML

 <a href="#" onclick="javascript:makeRequest('content.html','');">Load Content</a>
    <span id="result">
 <table id="myTable" valign="top" class="tablesorter">
        <thead>
          <tr>
            <th>Title 1</th>
            <th>Level 1</th>
            <th>Topics</th>
            <th>Resources</th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>Example title 1</td>
            <td>Example level 1</td>
          </tr>
          <tr>
            <td>Example title 2</td>
            <td>Example level 2</td>
          </tr>
        </tbody>
      </table>
</span>

The table is sorted using jquery table sorter plugin from http://tablesorter.com/docs/ After the ajax content is loaded, another set of table with different data will be displayed. However, the sorting doesn't work anymore.

Here is my ajax script which is use to load the content :

  var http_request = false;
   function makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      http_request.onreadystatechange = alertContents;
      http_request.open('GET', url + parameters, true);
      http_request.send(null);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
     // alert(http_request.status);
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('result').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }

Any idea how I can get the jquery plugins to work after the content is loaded? I have searched and changed the jquery.tablesorter.js click function into live() like this $headers.live("click",function(e) but it doesn't work as well.

How can I make the jquery functions to work after the content is loaded? Thank you


Update script for testing :

<a href="#" id="test" onClick="contentDisp()">Load Content</a>
<div id="result"></div>

<script type="text/javascript"> 
function contentDisp()
{
$.ajax({
url : "test.html",
success : function (data) {
$("#result").html(data);
$("#myTable").tablesorter();
$("#myTable").trigger("update"); 
}
});
}
</script> 

This my test.html. The table sorting works if I don't put it in loaded content. Once it's being loaded to div, the sorting doesn't work anymore.

<table id="myTable" valign="top" class="tablesorter">

    <thead>
      <tr>
        <th class="header">Title 1</th>
        <th class="header">Level 1</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>Example title 1</td>
        <td>Example level 1</td>
      </tr>
      <tr>
        <td>Example title 2</td>
        <td>Example level 2</td>
      </tr>
    </tbody>
  </table>      
A: 

You need to call $("#myTable").trigger("update"); after the table data is inserted.

Please take a look at this example on the tablesorter website, it may help you clean up your code for the loading of the content as well.

If you need more help / code, just post a comment. (I think it'll be better for you to figure it out instead of spoon-feeding that's why I'm not including it here.)

sirhc
Hello,Thanks so much for the answer. :) The table append works, but I do not intend to append the table. The issue is, it seems like all other jquery functions not working as well after a content is loaded using ajax. :(
Sylph
Have got the answer. Thank you so much for the headstart!I use this to call the function again after the the content is loaded and return success :)$(".tablesorter").tablesorter();
Sylph