0

I am trying to implement a button that will fire a search event when it is clicked and also when enter button is pressed. How to listen to the enter button press? This is the code for my button.

    <lightning-button class="slds-m-top_small"
    variant="brand"
    name="search"
    label="Search"
    onclick={handleSearch}>
    </lightning-button>

2 Answers 2

1

You can handle this by using onkeypress and checking that the pressed key is 13.

<lightning-input
        type="search"
          onchange={handleSearchKeyChange}
        label="Search..."
        value={searchKey}
        onkeypress={handleEnter}> 
 </lightning-input>

  <lightning-button class="slds-m-top_small"
variant="brand"
name="search"
label="Search”
onclick={handleSearch}>
</lightning-button>



 handleEnter(event){
    if(event.keyCode === 13){
      this.handleSearch();
    }
  }
2
  • sorry it is not working
    – adnan_aust
    Commented Apr 22, 2021 at 11:01
  • You need to put the onkeypress in your input tag. I updated my answer to show this.
    – Jordan
    Commented Apr 23, 2021 at 5:44
1

The Mozilla documentation says keypress is deprecated. Might be better to use keydown instead.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .