0

Can You help me find out how to use the html button element in JavaScript because

    document.write("<body> <div>  </div> </body>")

is not working for buttons or drop down menus so can you help me? my guess is the

    .write

Is the problem because that probably means text and a button is not text.

isherwood
  • 46,000
  • 15
  • 100
  • 132

2 Answers2

1

This is not the most correct way to do this. You must create elements and add them to the DOM tree.

let button = document.createElement('button');
document.body.append(button);

button.onclick = () => {
   alert('button clicked');
}
Isaac Bruno
  • 259
  • 1
  • 8
0

I think you want append(). This example is stolen from this MDN page

let parent = document.createElement("div")
let p = document.createElement("p")
parent.append(p)

console.log(parent.childNodes) // NodeList [ <p> ]
xdhmoore
  • 6,443
  • 6
  • 37
  • 71