[whatwg/dom] Implementing Document.prototype.createHTML (#996)

I am new to this particular part of the world. Please let me know whether this is the right place for this suggestion.

I would like to propose an addition to the `Document` prototype. This is to allow you to create a new element using HTML. This is consistent with other DOM properties and methods, such as `innerHTML` and `insertAdjacentHTML()`.

To illustrate the concept, I have this polyfill:

```js
Document.prototype.createHTML=html=>{
 let template=document.createElement('template');
 template.insertAdjacentHTML('beforeend',html);
 return template.lastChild;
};
```

You could then use it as follows:

```js
let img=document.createHTML('<img width="40" height="30" alt="alt text" title="title text">');
document.querySelector('h1').insertAdjacentElement('afterend',img);
img.src='https://resources.whatwg.org/logo.svg';
```

To create the `img` element above using traditional DOM methods requires half a dozen steps. You wouldn’t be able to take a shortcut via `insertAdjacentHTML()` since that doesn’t generate a separate element which can be accessed after the event, such as when you want to change the image’s `src`.

In comparison, jQuery has something similar in its multipurpose `jQuery()` function, but this proposal adds the feature to Vanilla JavaScript.

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/whatwg/dom/issues/996

Received on Saturday, 26 June 2021 03:31:52 UTC