Changing a <p> element's innerText

I need to change the InnerText of a <p> element in a p5.js script.

I tried:

setup {
  var myP = p.createP("change this");

draw {
  myP.innerText = "new text";

but this does not seem possible. Can the text of the element be changed after it has been created?

>Solution :

You need to declare the variable outside of the setup scope so that you have access to it in the draw method, additionally to set the inner text you can use the html method to set the inner html of the element.

var myP;
function setup() {
    createCanvas(400, 400);
    myP = createP("change this");

function draw() {
  myP.html("new text");

Leave a Reply