views:

195

answers:

1

I have a simple tree view that is loading child nodes through a ajah call to the server. I'm going to abbreviate the html but you should get the gist.

  <li id=1>Node 1</li>

When this is expanded (by being clicked on) there will be a bunch of sub nodes loaded through a ajah call (they are not on the page to begin with):

<ul>
  <li id=1_1>Node 1_1</li>
  <li id=1_2>Node 1_2</li>
  <li id=1_3>Node 1_3</li>
</ul>

and then again for Node 1_1

<ul>
  <li id=1_1_1>Node 1_1_1</li>
  <li id=1_1_2>Node 1_1_2</li>
  <li id=1_1_3>Node 1_1_3</li>
</ul>

Now that we got all that on the screen I want to have something like so (this is simplified to try and remain clear). I am using jQuery:

$('li').live('click', function() {
  var path = $('li').attr('id');
  var parent = '#1_1';  // this would be calculated, assuming 1_1_ node was clicked
  var grandParent = '#1'; // against calculated

  var crap = $(parent).text(); // should be 'Node 1_1'
  var darn = $(grandParent).text(); // should be 'Node 1'
});

Both crap and darn are not getting any values. I think this is because they aren't on the page and need a something like a "live" selector, similar to jquery's "live" events.

I think this answer on this question might be what I want, but not sure if it is the most efficient...so I posted a new question. I will try doing this and see what I come up with and post results. http://stackoverflow.com/questions/1081791/jquery-ajax-get-elements-inner-text

The issue though is I will have potentially 10,000 nodes visible on the screen at once so reloading and then filtering just seems slower than a direct getById type of selection.

A: 

I just tried the code as plain Html it works perfectly. As you mentioned if some of the "li" elements are not on the page, it will not fire for them.

So using jQuery's "Live query" Plugin you will not need to use the jQuery Click event handler again and again, also no need to filter or reload either.

http://plugins.jquery.com/project/livequery/

Rishav Rastogi
I've modified my question above, as I know that the HTML above would work fine IF it was on the page to begin with. Because the child nodes are not on the page until you click a parent node nothing is returned in the text methods. I can use the "live query" for clicking, but not sure how you think that will help be select the content that is loaded from that click.
rball
Crap...for some reason it wasn't working as expected before and now seems to be getting content that is loaded by an ajah request just fine. My bad.
rball