Ask coding questions

← Back to all posts
How to add html code to a page in javascript?
VulcanWM

How can I add html code to a page in javascript?
Thanks

Voters
VulcanWM
Comments
hotnewtop
xfinnbar

You shouldn't.

But you can. Use document.write() or set the innerHTML of the body, head or document.

VulcanWM

But I want one p element to have a function when you click on it and the rest not @xfinnbar

xfinnbar

@VulcanWM Then use onclick=functionName() in your HTML.

VulcanWM

yes but I don’t know which p element the function will be in @xfinnbar

ruiwenge2

@VulcanWM first, set an id for the p element and the onclick attribute for the button:

then do the javascript:

VulcanWM

i did that, but all of the code doesn't show.
Here is the repl link: https://replit.com/@VulcanWM/Odd-One-Out
and the code I want to show in the div is the code printed in the console
i tried changing the font size but nothing happened @ruiwenge2