Htmlly Htmlly - 1 month ago 9
Javascript Question

How to make text change upon user action?

How would I go about changing a

<p>
when the user clicks on an area?

I have my
<img>
, which has rectangular areas. Underneath the
<img>
is a
<p>
. Depending on which area has been clicked, I would like the text itself to change, like from 'change me' to 'i have changed'.

I want to change a paragraph text when user clicks on an element.

Thank you!! :D

Answer Source

let's assume that you have this HTML paragraph:

<p id="paragraph">change me</p>
<button id="btn" onclick="changeP()">Click here to change</button>

You need to make a Javascript function(changeP) as:

<script>
function changeP(){
document.getElementById("paragraph").innerHTML = "I have changed";
}
</script>