jQuery Examples - $("img").attr("alt", "Learn jQuery")



Description

This sets the alt attribute of all the images to a new value "Learn jQuery".

Syntax

Here is the simple syntax to use this method −

selector.attr(property,value)

Parameters

Here is the description of all the parameters used by this method −

  • property − This is the CSS property of the matched element.

  • value − This is the value of the property to be set.

Example

Following example would set alt attribute of the image to a new value "Learn jQuery"

Live Demo
<html>
   <head>
      <title>The jQuery Example</title>
      <script type = "text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js">
      </script>		
      <script type = "text/javascript" language = "javascript">
         $(document).ready(function() {
            $("#myimg").attr("alt", "Learn jQuery");
         });
      </script>
   </head>	
   
   <body>
      <div>
         <img id = "myimg" src="/jquery/images/jquery.jpg" alt="Sample image" />
      </div>
   </body>	
</html>

This will produce following result −

jqueryexamples_attributes.htm
Advertisements