2

I have 2 buttons in one form:

<input type="text" id="txtSearch" maxlength="250" placeholder="Enter Search" style="height: 42px; padding-right: 36px;" runat="server" />
<button type="submit" onserverclick="btnSearch_Click" id="btnSearch" runat="server"></button>

<input type="text" id="_searchKeyword" maxlength="250" placeholder="Search Term" style="height: 42px; padding-right: 36px;" runat="server" />
            <button type="submit" onserverclick="SearchButton" id="btnSearchNoResult" runat="server">
            </button>

My JavaScript:

$("#txtSearch").keyup(function (event) {
  if (event.keyCode == 13) {
    $("#btnSearch").click();
  }
}); 
$("#_searchKeyword").keyup(function (event) {
  if (event.keyCode == 13) {
    $("#btnSearchNoResult").click();
  }
});

if I type on _searchKeyword input text, and then press Enter button, I get the value of txtSearch input text. How can I get the value of _searchKeyword input text with enter press not value of txtSearch?

1 Answer 1

1

You need to add something like the code below to each of your inputs..

For the txtSearch...

onkeydown="if(event.keyCode == 13) doTxtSearch()"

For the searchKeyword...

onkeydown="if(event.keyCode == 13) doSearchKeyword()"

Full code changes below....

    <input type="text" id="txtSearch" maxlength="250" placeholder="Enter Search" style="height: 42px; padding-right: 36px;" runat="server" onkeydown="if(event.keyCode == 13) doTxtSearch()"/>
    <button type="submit" onserverclick="btnSearch_Click" id="btnSearch" runat="server"></button>

    <input type="text" id="_searchKeyword" maxlength="250" placeholder="Search Term" style="height: 42px; padding-right: 36px;" runat="server" onkeydown="if(event.keyCode == 13) doKeywordSearch()"/>
    <button type="submit" onserverclick="SearchButton" id="btnSearchNoResult" runat="server">
    </button>


    function doKeywordSearch()
    {
        //do Keyword Search stuff here
    }

    function doTxtSearch()
    {
        //do Text Search stuff here
    }
Sign up to request clarification or add additional context in comments.

5 Comments

in the searchKeyword onkeydown="if(event.keyCode == 13) doSearchKeyword()" and js function doSearchKeyword(){ $("#btnSearchNoResult").click(); }
@beginerdeveloper - that is not where it needs to be. Ill edit my answer so you will understand.
i did it but form not submit just refresh the page btnSearchNoResult not working
@beginerdeveloper - what does your new code look like, please post changes or I cannot determine what is wrong. Usually when it just refreshes the page there is a JavaScript error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.