Caballero
Caballero

Reputation: 12101

jquery .on parent selector

There used to be:

$("#element").parent().click(function() { alert("smth"); });

What's the equivalent of this with new jQuery event delegation on ?

$(document).on("click", "how to select #element parent here?", function() { alert("smth"); });

UPDATE: Actual code (although I don't see what this could change)

$(document).on("mouseover", ":has(>.hoverPhone)", function() {
    alert("smth");
});

"#hoverPhone" is a span in cells of one of the columns of jquery data table.

<tr class="odd">
    <td class=" sorting_1">05/11/2012</td>
    <td class="">Surname</td>
    <td class="">
        <span class="hoverPhone" title="Alternative No: 0123456789">9876543210</span>
    </td>
    <td class="">DIGITAL CAMERA</td>
    <td class="">6140</td>
    <td class="">CAMTRONICS</td>
    <td class="">clarify</td>
</tr>

This still doesn't work by the way.

UPDATE 2: Seems to be working. Personal comment, however... ":has(>.element)" really, jQuery? What's next? This is getting more and more confusing.

Upvotes: 3

Views: 1539

Answers (4)

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

The simplest I have :

$(document).on("click", ":has(>#element)", function(){...

Demonstration

Upvotes: 7

Reinstate Monica Cellio
Reinstate Monica Cellio

Reputation: 26143

If you're looking for delegation to handle dynamically added elements then you can't do it the way you propose. You need to handle the click event for all elements that could be the parent and check if they have the element in question as a child...

$(document).on("click", "*", function() {
    if ($(this).children("#element").length > 0) {
        alert("parent clicked");
    }
});

I'd strongly recommend changing document to a closer container element, and change * to the parent tag type, if possible.

Upvotes: 1

Rick Donohoe
Rick Donohoe

Reputation: 7271

This would still work fine:

$("#element").parent().on("click", function() {
     alert("smth"); 
});

Upvotes: 1

David Hedlund
David Hedlund

Reputation: 129792

You can still assign event listeners in the same manner:

$('#element').parent().on('click', function() { });

Upvotes: 2

Related Questions