jQuery Method How to - Use attr() to get attribute from tag








Question

We would like to know how to use attr() to get attribute from tag.

Answer


 <!--  w w w  . ja  v a  2 s. c om-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            var title = $("em").attr("title");
            $("div").text(title);
        });
    </script>
  </head>
  <body>
    <em title="title value">em</em>
    <div></div>
  </body>
</html>

The code above is rendered as follows: