The Best Way to Create HTML Elements with JavaScript?

preview_player
Показать описание
In today's video I'll be showing you my favourite way to generate HTML elements using JavaScript.

This technique allows you to pass in an HTML string and receive an HTML element back, meaning you then have access to things such as class list, event listeners, text content etc.

For your reference, check this out:

If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!

#dcode #javascript
Рекомендации по теме
Комментарии
Автор

Another technique I also like to use is return
I like using createContextualFragment instead of innerHTML because it's alot more faster and can run script tags.

rzgsqmn
Автор

Worth to mention, that using innerHTML is a security risk if the string to be inserted might contain potentially malicious content. When inserting user-supplied data you should always consider to sanitize the content before it is inserted.

fupdcfv
Автор

Не лучше ли использовать <template> в отдельных файлах html с подключенным в них файлах сценариев и стилей, чтобы затем подключить их в <head> директивой <meta import="...html>? Затем можно добавить их динамически методом appenChild, например.

JeanBotan
Автор

I literary was just looking for a video like this! That's why I'm subscribed to you!

sfmg
Автор

Very clear concepts. Thank you.
I have a single doubt regarding "template" parameter. I mean, what type of data si it treated like? as a string, as html tag? as a div using class="template"?.
Thank yo in advance. I hope you can respond

edmundogriffin
Автор

Hi guys! I have simple question.... How often do u use js to create some html element as web dev or use mostly writing code in html to create elements? Thanks for respons

danielsladecek
Автор

Solid video. Thanks for that 👍 im new to web development in general. For me its kind of "not the best way" to generate an element by passing the whole html string. In my studies/tryouts i found that the document.createElement("ul") and then pass the attributes like innerhtml, class etc by just dotting the element was more "handsome". Am i missing something here?
Best regards

teterposh
Автор

Another way is just to create template section with ID in your document and use that template in any place. For example dynamic created gallery.

apgeorgiev
Автор

I'm gonna cry I was so stuck you helped me out <3 Thank you so much bro God Bless you

aer
Автор

Is this meant to turn plain text of a website into interactive text or objects? Aside from making buttons in HTML, it seems to be just text until you add JS to interact with the site?

GetShwiftyInHere
Автор

Which code font family use in this video

rajankhunt
Автор

i can import html from another using async await fetch and use eval command

lonewolfcoding
Автор

2:45 is where I'm stuck when I put the html into the console and hit enter mylist shows up null

Hustlalyfe
Автор

As always very very nice content and exactly on point!

DoubleNation
Автор

I kinda got lost at the very end. How do I put the list (button in my case) in the place I want? I see puts it in the body but how would i put it in a div with an id

georegio
Автор

every time i code document.getelementbyId the console says document is not defined. how could i overcome this problem.

sangeethasancharaya
Автор

Thanks for this great tutorial. However, I did exactly like you but it didn't work! I have a script tag and within that tag I want to check a condition and if it's True I want to display an HTML form but for some reason it's not working with me!! Any idea how to do that please
It would help me a lot to finish my project

isamal-abbasi
Автор

If I create input element using javascript, how to get the value the user will put inside?

rmlacana
Автор

this only works if the html string argument has a single root node. if it’s a fragment with multiple root siblings, you’ll only get the first one back

derDooFi
Автор

Is it ok if I call you Document Object Model?

canepaper
welcome to shbcf.ru