Set attribute value

Description

The following code shows how to set attribute value.

Example


<html>
<head>
<script
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type='text/javascript'>
  $(document).ready(function() {<!--  ww  w  .ja  v  a  2  s .c om-->
    $('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>

Click to view the demo





















Home »
  jQuery »
    jQuery Example »




Animation
DOM
Event
Form
List
Select
Style
Table
Utilities