jQuery Selector How to - Select first element by element name








Question

We would like to know how to select first element by element name.

Answer


<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from  w  w  w.  j av a2 s.  c om-->
              var words = $("b:first").text().split(" ");
              console.log(words);
        });
    </script>
  </head>
  <body>
    <body>
       <b>Hello asdf asdf</b>
    </body>
</html>

The code above is rendered as follows: