I did not know I did not know - 28 days ago 6
HTML Question

Change the <p> content when img area is clicked

I need to know how to change the text inside of

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

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

Thanks :)

Answer

This is working example:

<html>
<head></head>
<body>

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

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

        // listeners
        img.addEventListener("click", function () {
           p.innerHTML = img.alt;
        });
    })();
</script>
</body>
</html>

and jsfiddle

https://jsfiddle.net/Lak2d6zw/1/