Note that there are some explanatory texts on larger screens.

plurals
  1. POjavascript .focus() won't work with tab
    primarykey
    data
    text
    <p>For some reason none of my setfocus() events were working on the server side of my web ap, so I created a javascript function setfocus that is set to trigger on the onload event of the body tag. </p> <pre><code>&lt;body onload="SetFocus()"&gt; </code></pre> <p>The user will click a radio button which enables the drop down. they select from the drop down which enables the textbox. When they scan a barcode into the textbox it will automatically tab out of the field, therefore triggering the onchange event to call some server side code which, if a successful SQL insert is completed, will insert a new row to the gridview, and it should set focus automatically back to the textbox via javascript. Once that gridview object has more than a header and a row of data, I disable the drop down and radio button list, so if you look at the javascript below, i can't use those as criteria in my if statement anymore. </p> <p>Javascript:</p> <pre><code> var MatAllRB = document.getElementById("HF_RB_MatAll").value; var MatAllCL = document.getElementById("DD_SelectCL_All").value; var MatAll = document.getElementById("Txt_MatBC_All"); var MatAllPrompt = document.getElementById("HF_AllPrompt").value; var count = $("#GV_Scan_All tr").children().length; </code></pre> <p>This works perfectly:</p> <pre><code>else if (ActiveTab == "cont-3" &amp;&amp; MatAllRB != "" &amp;&amp; MatAllCL != "") { alert("went into setfocus"); document.getElementById("Txt_MatBC_All").focus(); alert("made it through setfocus"); } </code></pre> <p>This only works if you actually click off the field, the tab doesn't work. It just does nothing</p> <pre><code>else if (ActiveTab == "cont-3" &amp;&amp; count == 9) { alert("went into setfocus"); document.getElementById("Txt_MatBC_All").focus(); alert("made it through setfocus"); } </code></pre> <p>This does the same thing as the above</p> <pre><code>else if (ActiveTab == "cont-3" &amp;&amp; (MatAllPrompt == "RP" || MatAllPrompt == "P03")) { alert("went into setfocus"); document.getElementById("Txt_MatBC_All").focus(); alert("made it through setfocus"); } </code></pre> <p>The weird thing is that all of these alerts that i have in here show up correctly, it just never sets focus. I am pretty new at this stuff still, so maybe i'm missing something, but I can't figure out why the focus() will work for click off rather than tabbing off. Any help would be appreciated.</p> <p>Here's the HTML</p> <pre><code> &lt;asp:RadioButtonList runat="server" ID="RB_Mat_All" AutoPostBack="true" RepeatColumns="4" OnSelectedIndexChanged="RB_Mat_All_OnSelectedIndexChange"&gt; &lt;asp:ListItem Text="NC" value="NC"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="DA" value="DA"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="GW" value="GW"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem Text="PC" value="PC"&gt;&lt;/asp:ListItem&gt; &lt;/asp:RadioButtonList&gt; &lt;asp:HiddenField runat="server" ID="HF_RB_MatAll"&gt;&lt;/asp:HiddenField &gt; &lt;br /&gt; &lt;asp:DropDownList runat="server" ID="DD_SelectCL_All" AutoPostBack="true" OnSelectedIndexChanged="DD_SelectCL_All_OnSelectedIndexChange"&gt; &lt;asp:ListItem Value="" Text="Select..."&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem value="53" text="05"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem value="54" text="06"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem value="55" text="07"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem value="57" text="08"&gt;&lt;/asp:ListItem&gt; &lt;asp:ListItem value="58" text="09"&gt;&lt;/asp:ListItem&gt; &lt;/asp:DropDownList&gt; &lt;asp:label runat="server" ID="Lbl_All_Prompt" CssClass="Form" Text=""&gt;&lt;/asp:label&gt; &lt;asp:HiddenField runat="server" ID="HF_AllPrompt"&gt;&lt;/asp:HiddenField &gt; &lt;asp:TextBox runat="server" ID="Txt_MatBC_All" Cssclass="Form" Text="" AutoPostBack="true" OnTextChanged="Txt_MatBC_All_TextChanged"&gt;&lt;/asp:TextBox&gt; &lt;asp:GridView runat="server" ID="GV_Scan_All" CssClass="Form_Feedback"&gt;&lt;/asp:GridView&gt; </code></pre> <p></p> <p>When I view the source the textbox looks like this:</p> <pre><code>&lt;input name="Txt_MatBC_All" type="text" onchange="javascript:setTimeout(&amp;#39;__doPostBack(\&amp;#39;Txt_MatBC_All\&amp;#39;,\&amp;#39;\&amp;#39;)&amp;#39;, 0)" onkeypress="if (WebForm_TextBoxKeyHandler(event) == false) return false;" id="Txt_MatBC_All" class="Form" /&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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