Changingb element part 1

JavaScript Tutorial
DOM & Events
Changing Elements
65
1/2
         

Changing Attributes



Once you have selected the element(s) you want to work with, you can change their attributes. 
As we have seen in the previous lessons, we can change the text content of an element using the innerHTML property.
Similarly, we can change the attributes of elements.
For example, we can change the src attribute of an image:<img id="myimg" src="orange.png" alt="" />
<script>
var el = document.getElementById("myimg");
el.src = "apple.png";
</script>

We can change the href attribute of a link:
<a href="http://www.example.com">Some link</a>
<script>
var el = document.getElementsByTagName("a");
el[0].href = "http://www.sololearn.com";
</script>
Try It Yourself

Practically all attributes of an element can be changed using JavaScript.

Comments