Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • jQuery - Selectors

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 218
    Comment on it

    jQuery Selectors are used to find out matching elements and select one or more HTML elements using jQuery and then perform operations on them.

    Factory function is a synonym of jQuery() function start with the dollar sign and parentheses $()

    The following three tags are used by $() for selecting elements

    1. Tag Name:-

    It represents a tag name present in your DOM.

    eg:-

    $('h2') selects all paragraphs <h2> in the document.
    

    2. Tag ID:-

    It represents a tag with the defined ID in your DOM.

    eg:-

    $('#some-id') selects the single element in the document that has an ID of some-id.
    

    3.Tag Class:-

    Tag class is very similiar to tag id it also represents a tag with the defined class in your DOM.but it selects all elements in the documents whereas tag id selects only a single element in the document.

    eg:-

    $('.some-class') selects all elements in the document that have a class of some-class.
    

    See this example to understand the use of selectors.

    Here in this example all the elements with a tag name p will be selected with font-size 15px and background red.

    <html>
    
       <head>
          <title>The jQuery Example</title>
          <script type = "text/javascript" 
             src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
         </script>
         <script type = "text/javascript" language = "javascript">
             $(document).ready(function() {
                $("p").css({"background-color":"red","font-size":"15px"});
             });
          </script>
       </head>
    
       <body>
          <div>
             <p class = "myclass">This is a paragraph.</p>
             <p id = "myid">This is second paragraph.</p>
             <p>This is third paragraph.</p>
          </div>
       </body>
    
    </html>
    

 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: