AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



html-javascript: How to change the attribute of a html tag by javascript?

I want to change the attribute value of a html element by javascript. Please give me sample code.

html x 24
javascript x 33
Posted On : 2013-12-06 23:49:16.0
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
up-rate
5
down-rate

Answers


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

Posted On : 2013-12-07 00:05:58
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250045
Reply This Thread
up-rate
5
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in