(ES6) ES6 (2016)

Return WebComponent (WC01)



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Web Component Example</title>
    <style>
        /* Global style */
        body {
            font-family: Arial, sans-serif;
            text-align: center;
            margin-top: 50px;
        }
        button {
            padding: 10px 20px;
            font-size: 16px;
            cursor: pointer;
        }
    </style>
</head>
<body>
<!-- Button to attach the Web Component -->
<button id="attach-button">Attach Web Component</button>

<!-- Root element where the Web Component will be attached -->
<div id="root"></div>

<script>
    // Define the Web Component
    class MyComponent extends HTMLElement {
        constructor() {
            super();
            // Attach a shadow root
            const shadowRoot = this.attachShadow({ mode: 'open' });
            shadowRoot.innerHTML = `
                    <style>
                        /* Scoped style */
                        p {
                            color: green;
                            font-weight: bold;
                        }
                    </style>
                    <p>This is inside a Web Component!</p>
                `;
        }
    }

    // Register the custom element
    customElements.define('my-component', MyComponent);

    // Add interaction: Attach the Web Component when the button is clicked
    const attachButton = document.getElementById('attach-button');
    const root = document.getElementById('root');

    attachButton.addEventListener('click', () => {
        // Create an instance of the Web Component
        const myComponent = document.createElement('my-component');

        // Append the Web Component to the root element
        root.appendChild(myComponent);

        // Disable the button after attaching the Web Component
        attachButton.disabled = true;
        attachButton.textContent = 'Web Component Attached';
    });
</script>
</body>
</html>





ES6 context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/JavascriptES6/WC01.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>