Note that there are some explanatory texts on larger screens.

plurals
  1. POWhen pressing enter in textbox, how do I focus on submit button using JQuery?
    text
    copied!<p>I have a page with multiple textboxes and each has a button associated with it. This works as a search 'hub', I suppose, where the user can search by ID, name etc.</p> <p>When entering a search term for e.g. name however, pressing enter 'clicks' the very first button on the page (ID search), causing an error if the associated textbox is empty (which it would be if someone meant to search by name).</p> <p>I have attempted to rectify this using JQuery, and have written the following:</p> <pre><code>$('input:text').click(function (e) { e.preventDefault(); $(this).next('input:button').focus(); }); </code></pre> <p>I'm simply using the click event so that I can monitor where focus is redirected, but eventually I would use .keypress. However, nothing happens upon clicking a textbox and I can't see for the life of me what is wrong with the JQuery.</p> <p>Any advice would be greatly appreciated!</p> <p>To elaborate on the problem, the .NET code used looks similar to the following throughout the page.</p> <pre><code>&lt;tr&gt; &lt;td style="width:100"&gt;Company ID&lt;/td&gt; &lt;td style="margin-left:3px;"&gt; &lt;asp:TextBox ID="...TextBox" runat="server" Width="230px"&gt;&lt;/asp:TextBox&gt; &lt;/td&gt; &lt;td&gt; &lt;asp:Button CssClass="Button" ID="...NameSearch" Text="Search" runat="server" /&gt;&lt;/td&gt;&lt;/tr&gt; </code></pre> <p>This is generating this (view source):</p> <pre><code>&lt;td&gt;&lt;input name="...TextBox" id="...TextBox" style="width: 230px;" type="text"/&gt;&lt;/td&gt; &lt;td&gt;&lt;input name="...NameSearch" class="Button" id="...NameSearch" onclick="javascript:__doPostBack('...NameSearch','')" type="button" value="Search"/&gt;&lt;/td&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload