jQuery HTML Element How to - Set src attribute from title attribute on the image








Question

We would like to know how to set src attribute from title attribute on the image.

Answer


 <!--from ww w  .j  a va  2s  .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", function() { 
                  return "/images/" + this.title; 
                });
        });
    </script>
  </head>
  <body>
      <img title="a.gif"/>
  </body>
</html>

The code above is rendered as follows: