MathCurvesSurfacesWallpaper GroupsGallerySoftwarePOV-Ray
ProgramingLinuxPerl PythonHTMLCSSJavaScriptPHPJavaLang DesignEmacsUnicode ♥

JavaScript: Set a Element's Attribute/Properties

,

This page shows you how to set a HTML/XML element's attribute, such as the values of {class, href, src, title, width, height, …}.

Use .setAttribute(‹attribute name›, ‹value›) to set a element's attribute. The ‹attribute name› should be a string. ‹value› should be a string.

If a attribute does not exist, it will be created.

Sample Code

HTML code:

<div id="id95680" class="xyz" title="important">important</div>

JavaScript code:

document.getElementById("id95680").setAttribute("title", "NO");

Test here: JavaScript test page: setAttribute.

As of , this works in all major browsers.

Reference

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

blog comments powered by Disqus