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>

1 Answer 1

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
    Apr 22, 2021 at 11:01
  • You need to put the onkeypress in your input tag. I updated my answer to show this.
    – Jordan
    Apr 23, 2021 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.