Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • jQuery - Methods to select elements by attributes

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 81
    Comment on it

    There are many times while selecting any element we do not have its class or ID, so in that case we have to take the help of various attributes to select the element. Suppose we have the following HTML

     

    <div>
    <a target="mainiframe" href="/profile/publicprofile/kanishka.kaushik/">kanishka.kaushik</a>
    </div>

     

    Here neither we have id nor class for the anchor tag, so to locate the element, we use the 'href' attribute of the anchor tag.

     

    $("a[href='/profile/publicprofile/kanishka.kaushik/']")

     

    In some situations we do not want to select that element that has some specific attribute then we will use the following format:

     

    $("a[attribute!='value']")

     

    If we want to locate elements that starts with some specific value then we will use the below jquery format. Use of carat symbol after the attribute will tell jquery that select only those elements whose attribute starts with the given value.

     

    $("a[attribute^='value']")

     

    The same is for the attributes that ends with some specific value.

     

    $("a[attribute$='value']")

     

    If we want only those elements that contains some text in their attribute value then we will use the following format:

     

    $("a[attribute*='value']")

     

    If we want to select only those elements whose attributes contains some specific word then we will use the following format

     

    $("a[attribute~='value']")

     

 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: