Web | This Site

jQuery Tutorial
jQuery UI
jQuery References
jQuery Resources
Selected Reading
© 2011 TutorialsPoint.COM

| Home |  | References |  | Discussion Forums |  | About TP

# jQuery - attr( key, fn ) Method

# Description:

The attr( key, func ) method sets a single property to a computed value, on all matched elements.

# Syntax:

Here is the simple syntax to use this method:

    selector.attr( key, func )

## Parameters:

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

- key: The name of the property to set.

- func: A function returning the value to set. This function would have one argument which is index of current element.

# Example:

Following example would create border for each table:

    <html>
    <head>
    <title>The Selecter Example</title>
    <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js">
    </script>

    <script type="text/javascript" language="javascript">

       $(document).ready(function() {

         $("table").attr("border", function(arr) {
              return arr;
            })

       });

    </script>
    </head>
    <body>

    <table>
    <tr><td>This is first table</td></tr>
    </table>

    <table>
    <tr><td>This is second table</td></tr>
    </table>

    <table>
    <tr><td>This is third table</td></tr>
    </table>

    </body>
    </html>

This would generate following result:

    <html>
    <head>
    <title>The Selecter Example</title>
    <script type="text/javascript" src="/jquery/jquery-1.3.2.min.js">
    </script>

    <script type="text/javascript" language="javascript">

       $(document).ready(function() {

         $("table").attr("border", function(arr) {
              return arr;
            })

       });

    </script>
    </head>
    <body>

    <table border="0">
    <tr><td>This is first table</td></tr>
    </table>

    <table border="1">
    <tr><td>This is second table</td></tr>
    </table>

    <table border="2">
    <tr><td>This is third table</td></tr>
    </table>

    </body>
    </html>

To understand it in better way you can Try it yourself.

Advertisement

Online Image Processing

Indian Baby Names