javascript domElement .is(cssSelector|":visible"|domElement)

 

javascript domElement .is(cssSelector|":visible"|domElement)


Target:

Applicable with any DOM element.

  • getElementById
  • getElementsByTagName
  • getElementsByClassName
  • getElementsByName
  • querySelector
  • querySelectorAll

Description :

return true if target element or first element of list is matched with selector other wise false .
selector : any css selector string
":visible" : string for check is visible. Same as isVisible() method
DomElement : matched with element if both are same then return true other wise false.


Syntax:

DOMElement.is(selector)

DOMNodeList.is(selector)

HTMLCollection.is(selector)


Usase:

  1. document.getElementById('firstDiv').is("div")
    it return true if firstDiv element is div other wise false
  2. document.getElementsByTagName('div').is(".FirstDiv")
    the getElementsByTagName selects all div but is() method check for only first div and if it has class Firstdiv then return true else false.
  3. document.getElementsByClassName('FirstDiv').is("div")
    the getElementsByClassName selects all elements having class "FirstDiv" but is() method consider only first element and return true if it is div other wise false.
  4. document.getElementsByName('FirstDiv').is("div")
    the getElementsByName selects all elements having name attribute "FirstDiv" but is() method consider only first element and return true if it is div other wise false.
  5. document.querySelector('.FirstDiv').is("div")
    the querySelector selects first div having class "FirstDiv" and is() method check whether it is div or not
  6. document.querySelectorAll('.FirstDiv').is("div")
    the querySelectorAll selects all elements having class "FirstDiv" but is() method consider only first element and return true if it is div other wise false.

  7. Other Examples:
  8. let firstDiv = document.getElementById('firstDiv');
    if(document.querySelector('#firstDiv').is(firstDiv)) //domElement match
    {
        alert('both are same');
    }
    else
    {
        alert("both are not matched");
    }
  9. if(document.getElementById('firstDiv').is(":visible")) //check visibility of element
    {
        alert('Element is Visible');
    }
    else
    {
        alert('Element Not Visible');
    }
  10. if(document.querySelector('.selected').is("li,div")) //css Selector match
    {
        alert("selected element is li or div");
    }
    else
    {
        alert("selected element is not li and div");
    }


Download MYJS.js

MyJs is a collection of java script extension methods that enhances the JavaScript objects, class object, DOM element, domElementsCollections, Date, String, Number and other data type. 

Include in you project landing page (master page) and it will extends your DOM and JavaScript Objects.

Comments

Popular posts from this blog

javascript : String.right

javascript : String.left

javascript : .nextAll()