Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
Lit element - onlick event in slotted child element

I have the following

    <custom-component>
     <div slot="form">
    <form>
    bunch of input elements
  <span slot="inner">  <button type="submit" id="submitButton"></button> </span>
    <button type="Reset"></button>
    </form>
    </div>
    </custom-component>

How do I listen to on click event the button type submit from the parent component custom-component? For reasons that I'm not going to to into, making the whole slotted form a component is not an option.

In the custom component file, this is what I tried

    <slot name='form' >
    <slot  name='inner' @click=${this.handleSearch}> </slot>

    </slot>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

It seems like you'll need to change a few things to make this work as intended.

First, the "inner" slot in your example isn't doing anything. You can't have a nested slot inside of light DOM rendered slot children. To test this out to see that this is true, you can try two things.

  1. Remove the <slot name="inner"> element, and you'll see that the "Submit" button still renders just fine in HTML.
  2. Second, the @click listener on the <slot name="inner"> never fires. Try moving it to the <slot name="form"> and you'll see that the callback fires as expected.

Another issue you'll run into is that the default browser Event being bubbled through the shadowRoot doesn't contain any information. If you console.log the data in the event callback, you'll see that all you get is {isTrusted: true} instead of the more useful event and element information.

Since you can't create the form within the web component, the other option is to just handle all of the logic with JavaScript in a <script> tag next to your <custom-component> in HTML. This makes sense since this is closer to the intended use of slotted children - to be able to add arbitrary HTML as children to your web components, but your web component isn't necessarily in charge of anything related to that HTML, it's just rendering it.

If you need to be in charge of the rendering, events, or logic related to the HTML children, they should be inside the web component, or else the logic needs to be handled outside of the web component.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!