0

 $("#AddDataStavka, #AddDataRazmer").on("keyup", function (event) { if (event.keyCode == 13) { e.preventDefault(); $("tr.trNewLine").children().first().children().first().get(0).click(); } }); /*This is code inside a document.ready, what I'm trying to do is call this every time a button is pressed and if its the enter key it should click the button. What am I doing wrong? Here's what I'm trying to target:*/
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <tr runat="server" id="trNewLine" class="trNewLine" visible="false"> <td runat="server" id="operationCol3"> <asp:LinkButton ID="btnAddDataAdd" runat="server" class="btn btn-pireus" OnClick="btnAddAddData_Click" ToolTip="Добави застраховка"><span>Добавяне</span></asp:LinkButton></td> <td> <asp:DropDownList runat="server" ID="AddDataType" class="DropDownListHint" data-taggle="dropdown" data-style="DropDownListHint-datastyle" OnSelectedIndexChanged="AddDataType_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> </td> <td> <input type="text" class="form-control form-control-pireus" id="AddDataStavka" clientidmode="static" onchange="glowInsuranceCheck()" runat="server" maxlength="15" text=''> </td> <td> <select class="DropDownListHint" id="AddDataRisk" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select> </td> <td> <input type="text" class="form-control form-control-pireus bst-suma" id="AddDataRazmer" clientidmode="static" onchange="glowInsuranceCheck()" runat="server" maxlength="19"> </td> <td> <select class="DropDownListHint" id="AddDataInsurer" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select> </td> <td> <select class="DropDownListHint" id="AddDataInsured" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select> </td> </tr>

5
  • Ok. So what is not working? What is the question? besides the e which is undefined What button are you trying to click? Commented Jul 11, 2017 at 11:13
  • e.preventDefault(); isn't acutally doing anything and it's clicing another button. Commented Jul 11, 2017 at 11:14
  • use event.preventDefault(); instead of e.preventDefault(); because in your code the e is not defined. Commented Jul 11, 2017 at 11:17
  • take a look on your browser console, you will see errors there Commented Jul 11, 2017 at 11:17
  • do u want to trigger it on button press? Commented Jul 11, 2017 at 18:56

3 Answers 3

1

as per your event handler function, e.preventDefault is wrong use event instead

 $("#AddDataStavka, #AddDataRazmer").on("keyup", function (event) { if (event.keyCode == 13) { event.preventDefault(); $("tr.trNewLine").children().first().children().first().focus().trigger("click"); } }); /*This is code inside a document.ready, what I'm trying to do is call this every time a button is pressed and if its the enter key it should click the button. What am I doing wrong? Here's what I'm trying to target:*/
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <tr runat="server" id="trNewLine" class="trNewLine" visible="false"> <td runat="server" id="operationCol3"> <asp:LinkButton ID="btnAddDataAdd" runat="server" class="btn btn-pireus" OnClick="btnAddAddData_Click" ToolTip="Добави застраховка"><span>Добавяне</span></asp:LinkButton></td> <td> <asp:DropDownList runat="server" ID="AddDataType" class="DropDownListHint" data-taggle="dropdown" data-style="DropDownListHint-datastyle" OnSelectedIndexChanged="AddDataType_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList> </td> <td> <input type="text" class="form-control form-control-pireus" id="AddDataStavka" clientidmode="static" onchange="glowInsuranceCheck()" runat="server" maxlength="15" text=''> </td> <td> <select class="DropDownListHint" id="AddDataRisk" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select> </td> <td> <input type="text" class="form-control form-control-pireus bst-suma" id="AddDataRazmer" clientidmode="static" onchange="glowInsuranceCheck()" runat="server" maxlength="19"> </td> <td> <select class="DropDownListHint" id="AddDataInsurer" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select> </td> <td> <select class="DropDownListHint" id="AddDataInsured" runat="server" data-placement="top" data-taggle="dropdown" data-style="DropDownListHint-datastyle"></select> </td> </tr>

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

5 Comments

I tried this and it's still not preventing the default.
@AlexanderHristov, check your console, maybe there are errors, and that's why the .preventDefault() is not even executing.
There's nothing inside the console, and it's not going into the event at all when I'm using debugger; Should I keep this code inside the document.ready?
@AlexanderHristov looks like the next line has some error, $("tr.trNewLine").children().first().children().first().get(0) is undefined.
I've changed it to this. $("tr.trNewLine").children().first().children().first().focus().trigger("click");
1

Use the following code instead. Use keydown/keypress event on the elements.

$('body').on("#AddDataStavka, #AddDataRazmer", "keydown", function (event) { if (event.keyCode == 13) { event.preventDefault(); $("tr.trNewLine").children().first().children().first().get(0).click(); } }); 

2 Comments

Same result, it's not reaching the function at all.
EDIT: It still didn't work tried both inside and outside the document.ready.
0

Try with selecting #btnAddDataAdd directly:

$("#AddDataStavka, #AddDataRazmer").keypress(function (e){ if (e.which === 13) { e.preventDefault(); $("#btnAddDataAdd"").click(); }; }) 

4 Comments

I've tried selecting it directly, it worked. The problem is it's inside an updatepanel and giving it clientidmode="static" makes it do a fullpostback.
And did you make 'btnAddAddData_Click' function? Just add this keypress event for Enter click inside that function.
Do you mean in c#?
No, with jquery. Like this, but then inside that function, you have to add what you want to happen when the button (enter) is pressed, assuming you want to add some data. And you have to add onclick="clickBtn()"

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.