How to create a new html element in javascript? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How to create a new html element in javascript?

I want to make a function where a new html element is created like a paragraph/div/list or anything i want. For example user presses a button and onclick i add the function. Then the function adds an html element to the code. How to make this happen

2nd Jun 2020, 6:58 PM
Asef Dian🇧🇩
Asef Dian🇧🇩 - avatar
6 Answers
+ 2
var p = document.createElement("p"); var node = document.createTextNode("Some new text"); //adding the text to the paragraph p.appendChild(node); anything.append(p); anything is the place where you want To put your paragraph
2nd Jun 2020, 8:04 PM
Mr Robot
Mr Robot - avatar
+ 1
var anything is not defined in this case, but Yes if you assosiate it with an element of DOM.
2nd Jun 2020, 8:37 PM
Leonid Selivanov
Leonid Selivanov - avatar
0
This is covered in this course: https://www.sololearn.com/Course/JavaScript/
2nd Jun 2020, 8:04 PM
Leonid Selivanov
Leonid Selivanov - avatar
0
https://www.sololearn.com/learn/JavaScript/2755/
2nd Jun 2020, 8:38 PM
Leonid Selivanov
Leonid Selivanov - avatar
0
Saad Mughal how do i give the element i just created an id?
3rd Jun 2020, 6:01 AM
Asef Dian🇧🇩
Asef Dian🇧🇩 - avatar
0
p.id = "Name"; I think like this we can set Id not sure try this Here p is a variable you created
3rd Jun 2020, 12:16 PM
Mr Robot
Mr Robot - avatar