MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaEmacsUnicode ♥
Web Hosting by 1&1

JavaScript: Get a Element's Attribute/Property Values

Xah Lee,

This page shows you how to get a HTML/XML element's attribute's values. For example, values of {class, style, href, src, title, width, height, …}.

Use ‹node›.getAttribute(‹attribute name›) to get a element's attribute value. The ‹attribute name› should be a string. The ‹node› is a HTML element object.

If a attribute does not exist, it returns null or "".

Sample Code

For example, if you have a HTML tag like this:

<div id="id46570"
class="xyz"
style="color:red;font-size:larger"
title="sample ele"
width="300px"
height="300px">sample element</div>

and you want to know its “title” attribue's value, do it like this:

var xx = document.getElementById("id46570");
var myTitle = xx.getAttribute("title");
alert("title is " + myTitle);

Test here: JavaScript test page: getAttribute.

As of , this works in all major browsers.

Reference

https://developer.mozilla.org/en/DOM/element.getAttribute

blog comments powered by Disqus