Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS equivalent of jQuery .is()

Is there a pure JS equivalent of jQuery .is() on modern browsers?

I know there is the querySelector method, but I want to check the node itself, rather than finding child nodes.

like image 786
Petah Avatar asked May 14 '14 02:05

Petah


3 Answers

Looks like matchesSelector is what I want.

https://developer.mozilla.org/en-US/docs/Web/API/Element.matches

Polyfill is here:

https://gist.github.com/jonathantneal/3062955

this.Element && function(ElementPrototype) {
    ElementPrototype.matchesSelector = ElementPrototype.matchesSelector || 
    ElementPrototype.mozMatchesSelector ||
    ElementPrototype.msMatchesSelector ||
    ElementPrototype.oMatchesSelector ||
    ElementPrototype.webkitMatchesSelector ||
    function (selector) {
        var node = this, nodes = (node.parentNode || node.document).querySelectorAll(selector), i = -1;

        while (nodes[++i] && nodes[i] != node);

        return !!nodes[i];
    }
}(Element.prototype);
like image 86
Petah Avatar answered Oct 05 '22 19:10

Petah


Another approach: Wrap the element you're testing in a parent then run querySelector from that

function is(el, selector) {
  var div = document.createElement('div');
  div.innerHTML = el.outerHTML;
  return div.querySelector(selector);
}

I ran one test and it worked:

JS

var a = document.querySelector('a');

 if(is(a, '.foo[name=foo]')) {
  console.log('YES');
} else {
  console.log('Nope');
}

HTML

<a href="#" class="foo" name="foo">Meow</a>

I am sure this can be done a lot prettier.

like image 23
Bill Criswell Avatar answered Oct 05 '22 19:10

Bill Criswell


You've already answered your own question, but as per my comment above I looked through the jQuery.fn.is function. This isn't a strip from the source, because the function they're using is more generalized so it can be used across multiple other functions, But I've boiled it down to this function:

function is(elem, selector){ //elem is an element, selector is an element, an array or elements, or a string selector for `document.querySelectorAll`
    if(selector.nodeType){
        return elem === selector;
    }

    var qa = (typeof(selector) === 'string' ? document.querySelectorAll(selector) : selector),
        length = qa.length,
        returnArr = [];

    while(length--){
        if(qa[length] === elem){
            return true;
        }
    }

    return false;
}

DEMO

like image 45
Adam Merrifield Avatar answered Oct 05 '22 18:10

Adam Merrifield