Element.hasAttribute

  • Revision slug: DOM/element.hasAttribute
  • Revision title: element.hasAttribute
  • Revision id: 79058
  • Created:
  • Creator: Jabez
  • Is current revision? No
  • Comment /* Syntax */

Revision Content

{{template.DomRef()}}

Summary

hasAttribute returns a boolean value indicating whether the specified element has the specified attribute or not.

Syntax

result = element.hasAttribute(attName) 
  • result holds the return value true or false.
  • attName is a string representing the name of the attribute.

Example

// check that the attribute exists before setting a value 
var d = document.getElementById("div1"); 
if d.hasAttribute("align") { 
  d.setAttribute("align", "center"); 
}

Notes

{{template.DOMAttributeMethods()}}

Specification

DOM Level 2 Core: hasAttribute

{{ wiki.languages( { "pl": "pl/DOM/element.hasAttribute" } ) }}

Revision Source

<p>
{{template.DomRef()}}
</p>
<h3 name="Summary"> Summary </h3>
<p><code>hasAttribute</code> returns a boolean value indicating whether the specified element has the specified attribute or not.
</p>
<h3 name="Syntax"> Syntax </h3>
<pre class="eval"><i>result</i> = <i>element</i>.hasAttribute(<i>attName</i>) 
</pre>
<ul><li> <code>result</code> holds the return value <code>true</code> or <code>false</code>.
</li><li> <code>attName</code> is a string representing the name of the attribute.
</li></ul>
<h3 name="Example"> Example </h3>
<pre>// check that the attribute exists before setting a value 
var d = document.getElementById("div1"); 
if d.hasAttribute("align") { 
  d.setAttribute("align", "center"); 
}
</pre>
<h3 name="Notes"> Notes </h3>
<p>{{template.DOMAttributeMethods()}}
</p>
<h3 name="Specification"> Specification </h3>
<p><a class="external" href="http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-ElHasAttr">DOM Level 2 Core: hasAttribute</a>
</p>{{ wiki.languages( { "pl": "pl/DOM/element.hasAttribute" } ) }}
Revert to this revision