<?xml version='1.0' encoding='UTF-8'?>
<?xml-stylesheet type='text/xsl' href='docs-1.7.xsl'?>
<entries><entry type='method' name="attr" return="String">
  <signature>
    <added>1.0</added>
    <argument name="attributeName" type="String">
      <desc>The name of the attribute to get.</desc>
    </argument>
  </signature>
  <desc>Get the value of an attribute for the first element in the set of matched elements.</desc>
  <longdesc><p>The <code>.attr()</code> method gets the attribute value for only the <em>first</em> element in the matched set. To get the value for each element individually, use a looping construct such as jQuery's <code>.each()</code> or <code>.map()</code> method.</p>
  	<p><strong>As of jQuery 1.6</strong>, the <code>.attr()</code> method returns <code>undefined</code> for attributes that have not been set. In addition, <code>.attr()</code> should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the <a href="http://api.jquery.com/prop/">.prop()</a> method.</p>
    <p>Using jQuery's <code>.attr()</code> method to get the value of an element's attribute has two main benefits:</p>
    <ol>
      <li><strong>Convenience</strong>: It can be called directly on a jQuery object and chained to other jQuery methods.</li>
      <li><strong>Cross-browser consistency</strong>: The values of some attributes are reported inconsistently across browsers, and even across versions of a single browser. The <code>.attr()</code> method reduces such inconsistencies.</li>
    </ol>
<blockquote><p><strong>Note:</strong> Attribute values are strings with the exception of a few attributes such as value and tabindex.</p></blockquote>
  </longdesc>
  <example>
    <desc>Find the title attribute of the first &lt;em&gt; in the page.</desc>
    <code><![CDATA[
var title = $("em").attr("title");
  $("div").text(title);
]]></code>
<css><![CDATA[
  em { color:blue; font-weight;bold; }
  div { color:red; }
]]></css>
  <html><![CDATA[
<p>
  Once there was a <em title="huge, gigantic">large</em> dinosaur...
</p>

  The title of the emphasis is:<div></div>
]]></html>
</example>
<category name="Attributes"/>
<category name="General Attributes"/>
<category name="Version 1.0"/>
<category name="Version 1.1"/>
<category name="Version 1.6"/>
</entry><entry type='method' name="attr" return="jQuery">
  <signature>
    <added>1.0</added>
    <argument name="attributeName" type="String">
      <desc>The name of the attribute to set.</desc>
    </argument>
    <argument name="value" type="String,Number">
      <desc>A value to set for the attribute.</desc>
    </argument>
  </signature>
  <signature>
    <added>1.0</added>
    <argument name="map" type="Map">
      <desc>A map of attribute-value pairs to set.</desc>
    </argument>
  </signature>
  <signature>
    <added>1.1</added>
    <argument name="attributeName" type="String">
      <desc>The name of the attribute to set.</desc>
    </argument>
    <argument name="function(index, attr)" type="Function">
      <desc>A function returning the value to set. <code>this</code> is the current element. Receives the index position of the element in the set and the old attribute value as arguments.</desc>
    </argument>
  </signature>
  <desc>Set one or more attributes for the set of matched elements.</desc>
  <longdesc><p>The <code>.attr()</code> method is a convenient way to set the value of attributes—especially when setting multiple attributes or using values returned by a function. Consider the following image:</p>
<pre>&lt;img id="greatphoto" src="brush-seller.jpg" alt="brush seller" /&gt;</pre>

    <h4 id="setting-simple-attr">Setting a simple attribute</h4>
    <p>To change the <code>alt</code> attribute, simply pass the name of the attribute and its new value to the <code>.attr()</code> method:</p>
    <pre>$('#greatphoto').attr('alt', 'Beijing Brush Seller');</pre>
    <p><em>Add</em> an attribute the same way:</p>
<pre>$('#greatphoto')
.attr('title', 'Photo by Kelly Clark');</pre>

    <h4 id="setting-several-attrs">Setting several attributes at once</h4>
    <p>To change the <code>alt</code> attribute and add the <code>title</code> attribute at the same time, pass both sets of names and values into the method at once using a map (JavaScript object literal). Each key-value pair in the map adds or modifies an attribute:</p>
<pre>$('#greatphoto').attr({
  alt: 'Beijing Brush Seller',
  title: 'photo by Kelly Clark'
});</pre>
    <p>When setting multiple attributes, the quotes around attribute names are optional.</p>
    <p><strong>WARNING</strong>: When setting the 'class' attribute, you must always use quotes!</p>
    <p><strong>Note</strong>: jQuery prohibits changing the <code>type</code> attribute on an <code>&lt;input&gt;</code> or <code>&lt;button&gt;</code> element and will throw an error in all browsers. This is because the <code>type</code> attribute cannot be changed in Internet Explorer.</p>
    <h4 id="computed-attr-values">Computed attribute values</h4>
    <p>By using a function to set attributes, you can compute the value based on other properties of the element. For example, to concatenate a new value with an existing value:</p>
<pre>$('#greatphoto').attr('title', function(i, val) {
  return val + ' - photo by Kelly Clark'
});</pre>
    <p>This use of a function to compute attribute values can be particularly useful when modifying the attributes of multiple elements at once.</p>
    <p><strong>Note: </strong>If nothing is returned in the setter function (ie. <code>function(index, attr){})</code>, or if <code>undefined</code> is returned, the current value is not changed. This is useful for selectively setting values only when certain criteria are met.</p>
  </longdesc>
  <example>
    <desc>Set some attributes for all &lt;img&gt;s in the page.</desc>
    <code><![CDATA[
$("img").attr({
  src: "/images/hat.gif",
  title: "jQuery",
  alt: "jQuery Logo"
});
$("div").text($("img").attr("alt"));
]]></code>
    <css><![CDATA[
  img { padding:10px; }
  div { color:red; font-size:24px; }
]]></css>
  <html><![CDATA[
  <img />
  <img />
  <img />

  <div><B>Attribute of Ajax</B></div>
]]></html>
  </example>
    <example>
      <desc>Set the id for divs based on the position in the page.</desc>
      <code><![CDATA[
$("div").attr("id", function (arr) {
  return "div-id" + arr;
})
.each(function () {
  $("span", this).html("(ID = '<b>" + this.id + "</b>')");
});
]]></code>
      <css><![CDATA[
  div { color:blue; }
  span { color:red; }
  b { font-weight:bolder; }
        ]]></css>
        <html><![CDATA[
  <div>Zero-th <span></span></div>
  <div>First <span></span></div>
  <div>Second <span></span></div>
]]></html>
  </example>
  <example>
    <desc>Set the src attribute from title attribute on the image.</desc>
    <code><![CDATA[
$("img").attr("src", function() {
    return "/images/" + this.title;
});
]]></code>
    <html><![CDATA[
<img title="hat.gif"/>
]]></html>
  </example>
  <category name="Attributes"/>
<category name="General Attributes"/>
<category name="Version 1.0"/>
<category name="Version 1.1"/>
<category name="Version 1.6"/>
</entry></entries>