I did not know I did not know -4 years ago 81
HTML Question

Change the <p> content when img area is clicked

I need to know how to change the text inside of

tag when img area is clicked. The text that should be set to the
tag is inside the area alt code. Can i extract the alt text and set it to

Here is the code: http://pastebin.com/DjNzg6Y9

Thanks :)

Answer Source

This is working example:


<p>Old Text</p>
<img src="http://placehold.it/300x300" alt="Content of alt">

    (function () {
        // definitions
        var p = document.getElementsByTagName("p")[0];
        var img = document.getElementsByTagName("img")[0];

        // listeners
        img.addEventListener("click", function () {
           p.innerHTML = img.alt;

and jsfiddle


Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download