The .setAttribute(‘attributeName’,’content’) method is used to set content to the attribute and .getAttribute(‘atrributeName’) method is used to get attribute value.
Example:
<!DOCTYPE html> <html> <head> <meta id="description" name="description" content="Free Web tutorials" /> <meta name="keywords" content="HTML,CSS,XML,JavaScript" /> <meta name="author" content="Hege Refsnes" /> <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> <script type="text/javascript"> function setProductMeta(){ document.getElementById("description") .setAttribute("content","testdesc"); var metaDesc=document.getElementById("description") .getAttribute("content"); document.getElementById("attr-test") .setAttribute("value","testvalue"); var textValue = document.getElementById("attr-test") .getAttribute("value"); } </script> </head> <body> <a href="#" onclick="setProductMeta()" >Click set&get Attribute</a> <input type="text" name="attribute-test" id="attr-test" value="" /> </body> </html>
Advertisements