0

I want to trigger button's click event when user types something in textbox. I have written the following textbox control and JQuery:

<asp:textbox id="description" runat="server" class="myclass">
<asp:Button id="btnApply" runat="server">

$(function () {
    $(".myClass").keyup(function (event) {
        $("#<%=btnApply.ClientID%>").click();
    });
});

When i enter first character in the textbox, the click event of button is triggered. But when i continue typing in the textbox the click event of button is not triggered on each key press. If i use keypress or keydown same issue is there along with textbox value is not retained. while when using keyup, the textbox is retained.

5
  • you need .trigger("click") .A simple search would have thrown many such results. Commented Mar 3, 2015 at 21:06
  • @roshan: not true. .click() calls trigger("click") internally. james.padolsey.com/jquery/#v=1.10.2&fn=jQuery.fn.click Commented Mar 3, 2015 at 21:12
  • @Mayur: is your page under inside updatepanel? unless this won't make sense. Commented Mar 3, 2015 at 21:13
  • why do you need $("#<%=btnApply.ClientID%>") instead of $('#btnApply') ? Commented Mar 3, 2015 at 21:17
  • @CanerAkdeniz: its framework related. it kinda muddles the id Commented Mar 3, 2015 at 21:19

2 Answers 2

0

Use trigger as shown below,

<asp:textbox id="description" runat="server" class="myclass">
    <asp:Button id="btnApply" runat="server">

    $(function () {
        $(".myClass").keyup(function (event) {
            $('#btnApply').trigger('click');
        });
    });

Also, add the click event and its functionality for the button.

Sign up to request clarification or add additional context in comments.

Comments

0

include it inside your document.ready

$(document).ready(

$(".myClass").on('keyup',function (event) {
    $("#<%=btnApply.ClientID%>").trigger('click');
});

You can aslo change "keyup" by "change" if you want to fire your event once the text edition has been finished

Comments

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.