Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading