Set attribute with the returned function value : attr « jQuery « JavaScript DHTML






Set attribute with the returned function value

 


<html>
  <head>
    <script type='text/javascript' src='js/jquery-1.3.2.js'></script>
    <script type='text/javascript'>
$(document).ready(
  function() {
    $('li').attr(
      'id',
      function() {
        return 'tmp' + $(this).text();
      }
    );
  }
);
    </script>
    <style type='text/css'>

ul {
    list-style: none;
    padding: 0;
    margin: 0;
}
ul li {
    margin: 3px;
    padding: 3px;
}
li#tmpA {
    background: #d7b05b;
}
li#tmpB {
    background: #d3988a;
}
li#tmpC {
    background: #8ad3a6;
}
li#tmpD {
    background: #8aa9d3;
}
    </style>
  </head>
  <body>
     <ul>
        <li>A</li>
        <li>B</li>
        <li>C</li>
        <li>D</li>
     </ul>
  </body>
</html>

   
  








Related examples in the same category

1.Disable input control
2.Get attribute from tag
3.Get input control type
4.Set Image URL title alt
5.attr(key, fn)
6.Sets src attribute from title attribute on the image.
7.attr(key, value) set a single property to a value, on all matched elements.
8.Finds the title attribute of the first in the page.
9.attr(name): access a property on the first matched element.
10.Disable all form input controls
11.Add attribute and clear attribute