user1032531
user1032531

Reputation: 26281

What causes jQuery objects to be different?

Why doesn't the first attempt to close the dialog work? Or maybe a better question is why is jQuery object $("#dialog") different than $(this).parent('div.dialog')?

$('#click').click(function() {
    $("#dialog").dialog("open");
});

$("#dialog").dialog({autoOpen:false}).find('li').click(function(){
    $(this).parent('div.dialog').dialog('close');
    $("#dialog").dialog("close");
});

    <div id="dialog">
        <ul>
            <li>111</li>
            <li>222</li>
            <li>333</li>
        </ul>
    </div>  

Upvotes: 1

Views: 41

Answers (1)

Travis J
Travis J

Reputation: 82287

$(this).parent('div.dialog') will only look up one level to see if the parent matches that selector. It will not traverse any further. The result is an empty set, and as a result using .dialog() has no effect.

What would work here would be closest jQuery API.

$(this).closest('div.dialog')

This would be the same as $("#dialog") in your example.

Upvotes: 1

Related Questions