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.

1
  • I would stop using document.write it has been surpassed by many much more useful manipulation methods. Commented Feb 22, 2021 at 16:59

2 Answers 2

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'); } 
Sign up to request clarification or add additional context in comments.

Comments

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> ] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.