skip to Main Content

I have a textbox for search items. I want to get items when type text in textbox and press enter key.

<input id="txt-search" placeholder="@StoreResource.SearchHere" class="form-control main-in">
    <a href="javascript:void(0);" id="btn-search" class="search-icon">
        <i class="icon feather icon-search"></i>
    </a>

I use this code but get empty value.

$("#txt-search").keyup(function (e) {
     var txt = $(this).val();

     if (e.key == "Enter") {
         alert(txt);
     }
})

2

Answers


  1. It is working as expected.
    One verify in below link stackbltiz

    If it is not expected , comment your requirement clearly.

    Login or Signup to reply.
  2. $("#txt-search").keypress(function (e) {
       if (e.which == 13) { // 13 is the Enter key
          var txt = $(this).val();
          alert(txt);
          $(this).val('');
        }
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
    <input id="txt-search" placeholder="@StoreResource.SearchHere" class="form-control main-in">
        <a href="javascript:void(0);" id="btn-search" class="search-icon">
            <i class="icon feather icon-search"></i>
        </a>

    This code works as expected. When entering the value into the text box and press enter then get the value which is entered into the textbox.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search