jQuery: referenciar y manipular el elemento principal de un elemento agregado dinámicamente

I'm loading some XML in my document and add a DIV-button to it. By clicking the button, I wan't to fadeOut() not only the button, but also its parent element. While referencing and fading out the button itself is working, I fail to get access to its parent-element. (see the last lines in my code).

$(document).ready(function(){

    var searchstring = decodeURIComponent(getUrlVars()["search"]);

    $.ajax({
        url: 'data.xml',
        type: 'GET',
        dataType: 'xml',
        timeout: 10000,
        error: function(){
            alert('Error loading XML document');
        },
        success: function(xml){
            $(xml).find('entry').filter(function(index) { return $('lemma', this).text() == searchstring; } ).each(function() { 
                $(this).find('part_gp > part').each(function(){
                    var more = $('<div class="more">+</div>');
                    $(this).after(more);
                    more.click(fader);
                });

            $( "#result" ).append( $(this) );

            });
            }
    });
});

function fader() {
    $(this).fadeOut();   // this works!!
    $(this).parent().fadeOut(); // this doesn't work!!
}

ACTUALIZACIÓN:

The XML/HTML will look like this:

<part_gp>
  <part>Grafschaft</part>
  <div class="more">+</div>
  <pos> feminin / weiblich</pos>
</part_gp>

I want to hide the whole part_gp..

$(this).parents("part_gp").fadeOut();

didn't work either..

preguntado el 02 de febrero de 12 a las 10:02

2 Respuestas

El problema es que estás usando $(this).after(). That is putting your button after the block you want to hide, not inside of it. What you really want is $(this).append().

http://api.jquery.com/after/
http://api.jquery.com/append/

Respondido 02 Feb 12, 14:02

i dont know how the markup looks like but you can try

$(this).parents("div.someClassForIdentification").fadeOut();

Respondido 02 Feb 12, 14:02

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.