jQuery HTML Element How to - Get Image alt message








Question

We would like to know how to get Image alt message.

Answer


<!--from   w w  w .  j  a 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(){
             $("img").attr({ 
                 src: "http://www.java2s.com/style/download.png",
                 title: "java2s.com",
                 alt: "Logo"
             });
             $("div").text($("img").attr("alt"));
        });
    </script>
  </head>
  <body>
    <body>
      <img />
      <div></div>
  </body>
</html>

The code above is rendered as follows: