views:

47

answers:

2

hi,

is it possible that javascript doesnt apply to elements created trough an ajax request? practically i have a tree of elements like parents and children with a dept of more levels.

i have the root elements on the index page and on click i can retrive the children trough this request:

var get_children = function() {
 pid = $(this).attr("id");
 //var parentid = pid
    // store value in data variable
    var data = { par: pid };
    $.getJSON("/holz/children/",data,
        function(data){
      //remove the box if it already exists
      $("#parid-" + pid ).remove();
            // Add the messages div to the container
            $("#container").append("<div class='box' id='parid-" + pid + "'></div>");
            //create the id set for the box
            boxid = "#parid-"+pid
            //insert the elements one after each other with the id set to the elements pk  
            $.each(data, function(i,item){
                $(boxid).append('<p><a '+'id="'+item.pk+'"'+' class="element" href="#">'+item.fields.title +' ( id = '+ item.pk+' )'+'</a>'+'</p>');
            });
        }
    );
  return false;
};

the problem is that i cant go deeper because the request doenst apply on the elements i got from the first request. the ajax request calls a django view which should (and it does in the on the first element) and returns a json reponse which i use to create a box with the children.

what am i doing wrong?

thx

A: 

Nope, JavaScript always applies to every valid element in the DOM, no matter where it came from.

I can't really make much sense out of your code yet, but maybe your AJAX call is injecting elements with IDs that already exist in the document? That would cause trouble when trying to address those elements.

Pekka
every <a> has an id created by the elements primary key so it there cant be any duplicated id elements. the js grabs the id value and takes it to retrieve the elements with that parent. i looked at the code a few times and i cant find the problem... the function is fired on the click event on the <a>.
aschmid00
You are not giving the newly arriving child elements any onclick event they could listen to, are you?
Pekka
You need to re-apply whatever function you use to make your links clickable. At the moment, what you get from the AJAX call is just dead links without any attached action (which is by design).
Pekka
ok... but the click event to the <a> with a class of element is loaded at the first page load... $(document).ready(function(){ $(".element").click(get_children);});do i have to reapply it again?
aschmid00
Yup, that only applies to the `.element` s that are present at the time you run the function. It is not a general rule for all .elements.
Pekka
+1  A: 

I'm not sure I fully understand but it sounds like you want an onclick eventhandler to apply to all domelements with a certain css class .element. Am I right?

If so then just use jQuery's live() event binder syntax. This will allow you to bind an event to all dom elements that match a given selector now, and in the future.

To use some of your own code as an example:

$('div.box').live('click', function() {
    alert('you clicked me!');
});

$("#container").append("<div class='box' id='parid-" + 1 + "'></div>");

In the example above the div that we added dynamically will have our click event bound to it since we used the jquery api to insert it.

Jared
Agreed, dynamically added elements aren't wired up because that wire up code has already been run, so you have to use live in order to ensure added elements are also wired up.
TreeUK
thx guys... this worked.
aschmid00