JavaScript Shorthand for extending native DOM element?

As explained in mdn, custom elements may be based on a native element. For Example, <button is="my-button">. What I'm looking for is kind of the opposite syntax. I'd like a shorthand for creating an element whose tag is custom and is based on a native element, such as <purchase-button is="button">. I'm not looking at add functionality to this custom element over what HTMLButtonElement provides. Just the name.

Yet, this doesn't work:

let purchaseButton = document.createElement('purchase-button', { is: 'button' })
document.body.appendChild(purchaseButton)

The resulting element does not extend HTMLButtonElemenet.

Answer:1

It's not possible, and it doesn't exist in the Custom Element specification.

About this requirement, see issue 603 in the specification forum.

Some are discussing similar needs in other issues (for example #509, good luck for the reading...).

As a workaround, define a <purchase-button> custom element into which you insert a <button>. Then you can use a Shadow DOM with the <slot> element to reflect the original content of the <purchase-button>.

customElements.define( 'purchase-button', class extends HTMLElement {
    constructor() {
        super()
        this.attachShadow( { mode: 'open' } )
            .innerHTML = '<button><slot></slot></button>'
    }
} )

PB.onclick = ev => 
  console.info( '[%s] clicked', ev.target.textContent )
<purchase-button id=PB>Pay</purchase-button>
Answer:2

I'm using the awesome Slick slide on a project. The problem is: I have a carousel with 5 divs inside. 4 images and a video. On mobile (< 768px) I want the slider to show only the images, not the ...

I'm using the awesome Slick slide on a project. The problem is: I have a carousel with 5 divs inside. 4 images and a video. On mobile (< 768px) I want the slider to show only the images, not the ...

  1. remove slick slider jquery
  2. remove slick slider
  3. slick remove slide
  4. slick remove slide by class
  5. slick remove first slide
  6. slick remove last slide
  7. slick slider remove arrows
  8. slick slider remove slide
  9. slick slider remove clone
  10. slick slider remove outline
  11. slick slider dots remove numbers
  12. slick slider remove buttons
  13. slick slider remove arrow text
  14. slick slider remove blue outline
  15. slick slider remove dots
  16. slick slider remove tabindex
  17. slick slider remove draggable
  18. slick slider remove all slides
  19. slick slider remove transform
  20. slick slider remove animation

I have this button <button id="<?php echo $u['id']?>" name="activation" onclick="handleButton(this);" type="submit" class="btn btn-success"></button> And this button related to ...

I have this button <button id="<?php echo $u['id']?>" name="activation" onclick="handleButton(this);" type="submit" class="btn btn-success"></button> And this button related to ...

  1. send value controller to view
  2. send value from controller to view mvc
  3. send value from controller to view codeigniter
  4. send value from controller to view laravel
  5. send value to controller mvc
  6. send value to controller angularjs
  7. send value from controller to javascript
  8. send value from controller to jsp
  9. send value to controller ajax
  10. send value to controller
  11. send value to controller jquery
  12. send value from one controller to another
  13. send checkbox value to controller mvc
  14. send textbox value to controller mvc
  15. send value from javascript to controller mvc
  16. send button value to controller
  17. send dropdown value to controller using ajax
  18. thymeleaf send value to controller
  19. send value from jsp to controller spring
  20. send checkbox value to controller mvc c#

How do I use enums in ES2015? I tried the following but I'm getting errors: Created a file for my enums i.e. errorTypes.js export const Types = { MISSING_DATA: 0, INVALID_DATA: 1 } I then ...

How do I use enums in ES2015? I tried the following but I'm getting errors: Created a file for my enums i.e. errorTypes.js export const Types = { MISSING_DATA: 0, INVALID_DATA: 1 } I then ...

While attempting to use React.js and maybe or most likely I am doing something wrong? But when I run React.Render() nothing visible renders, however. Viewing the DOM through Chromes console I can see ...

While attempting to use React.js and maybe or most likely I am doing something wrong? But when I run React.Render() nothing visible renders, however. Viewing the DOM through Chromes console I can see ...