We can change the attribute value by javascript. setAttribute(´attribute-name´,´value´) is used to set the value of an attribute. and getAttribute(´attribute-name´) is used to get the attribute´s value. Let´s see the simple example of changing the html tag attribute´s value by javascript.
****************** Example to change the attribute of a html tag by javascript ******************
dynamic.html :
--------------------
<script type="text/javascript">
function change(){
if(document.getElementById("change").getAttribute("color")=="red"){
document.getElementById("change").setAttribute("color","navy");
}
else{
document.getElementById("change").setAttribute("color","red");
}
}
</script>
<font color="red" id="change">Hello</font>
<input type="button" value="Change color" onclick="change()" />
================================= output =================================
Hello
5