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

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 256
    Comment on it

    CSS selectors do not provide us the facility to interact with the content of any element. So in that case we have to depend either on xpath or jquery selector to select elements on the basis of contents.

     

    In jQuery selector we have various methods to select element based on the content.

     

    Suppose we have the following HTML

     

    <ul>
    <li>Type in your tech analysis, product insights and tech solutions.</li>
    <li>Record your Desktop for better insights.</li>
    <li>The recording is saved on cloud.</li>
    <li>Share the link.</li>
    </ul>

     

    Now we have to select that li which contains the text "Share the link"

     

    $("li:contains('Share the link')")

     

    Note: contains() method is case-sensitive and will not match the content if we pass the argument as "share The Link". So, the exact content should be passed when we use contains() method.

     

    If we have to select those elements that do not contains other elements then we have to use jquery as in the below format. Suppose we want those <ul> that do not contain anything:

     

    $("ul:empty")

     

    This will select all the ul that do not contains anything.

 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: