Szymon Toda
Szymon Toda

Reputation: 4516

Select 2nd ancestor of clicked element

How to get 2nd ancestor of clicked element?

It returns nothing: $(this).parent().parent().find('.TITLE'), $(this).closest('.TITLE').text()

It referrs to this code:

<div class="ELEMENT_LISTY" style="display:none">
<div class="TITLE" id="id_196">sadfsfsdfgds</div>
<div class="BUTTONY PRE">
<div class="POGLAD"></div>
<div class="ZMIEN_NAZWE"></div>
<div class="USUN"></div>
</div>
</div>

function przygotuj_ZmianeNazwy() {
    $(".PRE .ZMIEN_NAZWE,.NEW .ZMIEN_NAZWE").click(function() {

        $("#DIALOG3").dialog({
            resizable: false,
            width: 500,
            modal: true,
            show: 'scale', 
            hide: 'puff',
            buttons: {
                "Dodaj": function() {
                    var el = $("#input_ZmienNazwe").val();
                    if(el !== '' && el !== null) {
                        $(this).dialog("close");
                        var title = $(this).closest('.TITLE').text() // I AM CLICKING ON ELEMENT AMONG MANY, WANT TO SELECT ANCESTOR TEXT FROM THIS I CLICKED ON
                        alert(title);
                        //ajax_zmienNazwe(title);
                    }
                },
                "Anuluj": function() {
                    $(this).dialog("close");
                    $("#input_ZmienNazwe").blur();
                }
            }
        });
        document.getElementById("input_ZmienNazwe").focus();
    });

Upvotes: 0

Views: 92

Answers (1)

samy-delux
samy-delux

Reputation: 3069

I've created a jsFiddle which works here http://jsfiddle.net/stJHG/1/. You need to adapt your code to the following:

$(".PRE .ZMIEN_NAZWE,.NEW .ZMIEN_NAZWE").click(function() {
    alert($(this).closest('.ELEMENT_LISTY').find('.TITLE').text());        
});​

.TITLE is not actually an ancestor of your element, but .ZMIEN_NAZWE and .TITLE have a common ancestor which is .ELEMENT_LISTY. So you have to select this one and then search it's children for .TITLE.

(Also I don't think your style="display:none" was in the place where you ment it to be, the clickable element was hidden as well)

Upvotes: 3

Related Questions