Jquery: bind function to elements not yet loaded

I attach a function to each th element of a specific table:

$(document).ready(function()
{
    $('#table_id thead th').each(function()
    {
       $(this).disableTextSelect(); // disableTextSelect() is user defined function extending jquery
    });
}

This works great if the table is already loaded. To attach it to the table, if it will be loaded in the future, I have to use .on (). But I dont know how: - (

$(document).on('which event?!', '#table_id thead th', function()
{
    $(this).disableTextSelect();
}); 

Thanks a lot Phantom

+3
source share
4 answers

I assume you want to bind an event to a click?

$(document).on('click', '#table_id thead th', function() {
    $(this).disableTextSelect();
}); 

In the future, links in jQuery documents contain a list of all jQuery events that can be found here .

+2
source

, .delegate("","load"), delegate load,unload,change,blur,focus, bubble, delegate . < > click, .
load, , setInterval :

var itv = setInterval(function(){
    var el = $("#table_id");
    if(el.length > 0){
        el.find('thead th').each(function(){
            $(this).disableTextSelect();
        });
        clearInterval(itv);
    }
},100);

u.:)

+1

, .

AJAX, , .ajax() ( .load() ..), .success() .

, . AJAX, , , .

0

-, , , . , .

function somename() {
  $('#table_id thead th').each(function()
    {
       $(this).disableTextSelect(); // disableTextSelect() is user defined function extending jquery
    });
}

call somename()once to activate it for the currently loaded elements, and call it every time when loading new elements that will correspond to the selector. I know this will work, but I do not know how to use it effectively.

0
source

All Articles