RSS

How to get/set element attribute value in javascript

13 Dec

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>
 
Leave a comment

Posted by on December 13, 2012 in General, Javascript

 

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: