Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery's closest doesn't work in IE8/9

I have this jQuery code:

$(this).closest('div:has(.FIND_ME)').find('.FIND_ME').hide();

But element with class .FIND_ME doesn't hide in IE8 and 9.

This question is a continuation of Search for an item with a common ancestor

HTML:

<div>
    <div><!-- all div without ID -->
        <span>some text</span>
        <div>
          <span id="listener1">click here</span>
          <span>sometext</span></div>
        <div>

        <span class="FIND_ME">Result Here</span></div>
    </div>

    <div>
        <span>some text</span>
        <div id="div1">
         <div id="div2">
          <span id="listener2">click here</span>
          <span>sometext</span></div>
         </div>
        <div>

        <span class="FIND_ME">Result Here</span></div>
    </div>
</div>
like image 733
DmitMedv Avatar asked Jan 20 '15 08:01

DmitMedv


2 Answers

I was setting a variable element to this, then later on I was calling:

element.closest('a')

But element was now the DOM element instead of the jQuery object. So changing to:

$(element).closest('a')

fixed it.

like image 108
BenG Avatar answered Nov 18 '22 19:11

BenG


closest = function (target, tag) {
    if (target.parentElement == "undefined") {
        return null;
    }
    if (target.parentElement.localName == tag) {
        return target.parentElement;
    }
    return this.closest(target.parentElement, tag);
};
like image 1
elpezganzo Avatar answered Nov 18 '22 20:11

elpezganzo