0

I have a table on the page that's loaded when I click search button. The table loads dynamically and it doesnt exist in the layout. I wonder how to select this table inside a function to do some other stuff with it. I tried something like this to select a column:

$(function (){
var elem = $('tbody').find('td');
});

But I'm not sure how to call this tbody if it doesn't exist in the layout yet. How to do that?

JDoeBloke
  • 551
  • 4
  • 7
  • 19

1 Answers1

2

The dilemma is, $(function() { is short for $(document).ready(function() { ($(document).ready), which means you try to get the content of tbody when the document is ready, but your table isn't present at that time.

You have to create an event handler, that executes whenever the content is loaded in dynamicly, which can be done like this:

$(document).on("change", "table", function() {
    var elem = $("tbody").find("td");
}

(JQuery on)

$(document).on("change" is checking whenever the document is changed, "table" means when the object in the document that is changed is a table, do what is specified in the function()

I hope this explains your dilemma