Astudent
Astudent

Reputation: 196

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?

Upvotes: 1

Views: 190

Answers (1)

White Wizard
White Wizard

Reputation: 596

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");
}

Upvotes: 3

Related Questions