Ask coding questions

← Back to all posts
help help help
MatthewX (139)

How do I write something in html then be able to change it in java script? Also, how do I do a random choice from a list of objects? Finally, how do i die imgs/ buttons in html then make them appear when a button is clicked?

Answered by SixBeeps (2907) [earned 5 cycles]
View Answer
Commentshotnewtop
SixBeeps (2907)

Each HTML element has certain functions and fields that you can tinker with. For example, let's say you had the following HTML:

<p id="myPara">This is a paragraph</p>

You can then change the contents by editing the <p> tag's innerText variable:

let myPara = document.getElementById("myPara"); // You should only call this once at the top of your JS file
myPara.innerText = "This is the same paragraph, but with new text!";

As for making things appear/disappear, you can set the element's style's display property.

myPara.style.display = "none"; // Makes the paragraph disappear
myPara.style.display = "block"; // Makes the paragraph reappear

Picking from a list of objects requires knowledge of arrays. Once you understand how they work, you can then access a random element by calling Math.random() and getting that index.

MatthewX (139)

@SixBeeps Wow you good at html. Thx!

MatthewX (139)

@SixBeeps Also, where should i go to learn how to randomly pick from a list?

MatthewX (139)

@SixBeeps will the hiding thing work on imgs and buttons?

hg0428 (167)

Learn on codecademy, khanacademy, or many others. @MatthewX