Home > Net >  Changing a <p> element's innerText
Changing a <p> element's innerText

Time:01-03

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?

CodePudding user response:

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() {
  background(220);
  myP.html("new text");
}
  • Related