jQuery Examples - $("#myID").attr("custom")



Description

This would return value of attribute custom for the first element matching with ID myID.

Syntax

Here is the simple syntax to use this method −

selector.attr(property)

Parameters

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

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

Example

Following example would show the value of custom attributes of <p> tags;

Live Demo
<html>
   <head>
      <title>The Selector 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() {
            $("#result1").text( $("p#pid1").attr("custom") );
            $("#result2").text( $("p#pid2").attr("custom") );
         });
      </script>		
      <style>
         .red { color:red; }
         .green { color:green; }
      </style>		
   </head>	
   
   <body>
      <p custom="red" id = "pid1">This is first paragraph.</p>
      <p custom="green" id = "pid2">This is second paragraph.</p>
      <div id = "result1"></div>
      <div id = "result2"></div>
   </body>	
</html>

This will produce following result −

jqueryexamples_attributes.htm
Advertisements