Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to check an element is exists or not?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 226
    Comment on it

    In JQuery to check that element is exists or not we use length() function. Using length() function we will check the length of the selector, if it returns something then the element must exists else not.

     if( $('#selector').length )         // to check the id selector
    {
         // exists
    }
    
    
     if( $('.selector').length )         // to check the class selector
    {
         // exists
    }
    

    For example,

    <h1 id="heading">This is a Heading</h1>
    <p>This is a paragraph.</p>
    
    

    Suppose we have to check the element with id heading exists or not. For this, we will use the following code:

     if( $('#heading').length )  {
    alert("exist");
    }else
    {
    alert("does not exist");
    }
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: