You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is a different approach using bacticks strings that we learn earlier, there is a potential security hole related to XSS (Cross-site scripting), that we learn how to prevent on the Security Section ofthis course.
362
+
363
+
Similar to ```innerText``` awe can use **```innerHTML```** as a setter, using strings instead of regular document.createElement, with```innerHTML``` you can create tags, also we can interpolate values easily, and if its a valid HTML the browser will take it and parse it into all the items:
364
+
365
+
Example:
366
+
we have the following HTML and we want to add an image with source and description, that will be inside a ```div```with a classor wrapper:
367
+
368
+
```HTML
369
+
<article class="item">
370
+
371
+
</article>
372
+
```
373
+
In javascript:
374
+
375
+
```js
376
+
const item = document.querySelector('.item');
377
+
378
+
const src = `https://picsum.photos/200`;
379
+
constdesc=`cute pup`;
380
+
381
+
constmyHTML=`
382
+
<div class="wrapper">
383
+
<h2>${desc}</h2>
384
+
<img src="${src}" alt="${desc}"/>
385
+
</div>
386
+
`;
387
+
```
388
+
389
+
But these are not elements are strings, you can check by using
390
+
```typeof myHTML```, meaning that we can't add classes, it only becomes an element by dump it into the DOM by setting the ```innerHTML``` like:
391
+
392
+
```js
393
+
item.innerHTML= myHTML;
394
+
```
395
+
Now you can add classes or whatever you want.
396
+
397
+
There is a ways you can do the same without ```innerHTML``` is:
0 commit comments