Add attribute and clear attribute : attr « jQuery « JavaScript DHTML






Add attribute and clear attribute

 

<html>
  <head>
    <script type='text/javascript' src='js/jquery-1.3.2.js'></script>
    <script type='text/javascript'>
$(document).ready(
  function() {
    $('div').hover(
      function() {
        $(this).attr('id', 'justAdd');
        $(this).text('This element\'s ID is: ' + $(this).attr('id'));
      },
      function() {
        $(this).attr('id', '');
        $(this).text('This element\'s ID has been removed.');
      }
    );
  }
);

$('a').removeAttr('title');
    </script>
    <style type='text/css'>
div {
    width: 350px;
    padding: 10px;
    border: 1px solid rgb(200, 200, 200);
    background: #93cdf9;
    margin: 5px;
}
div#justAdd {
    background: #6faddd;
}
    </style>
  </head>
  <body>
     <div>Mouse over to change this element's id.</div>
  </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.Set attribute with the returned function value