0

I want to focus a textbox when web page is loaded. These codes are executed on the PC browser(Google chrome,internet explorer etc.) But if you are entering on the Ipad browser, it does not work. What is the problem ? this subject is very important for me. Please..

  <asp:TextBox ID="txt_arama2" runat="server" Width="60%"></asp:TextBox>    

  window.onload = function() {
  document.getElementById("txt_arama2").focus();
  };

1 Answers1

0

The problem is, when the asp Textbox is rendered, the ID will change it's name to something like this:

ctl00$m$g_5283b87a_5e59_4ac5_8266_7c4bd2119e8d$ctl00$txt_arama2

Try to add a class instead and access the element with the class attribute.

<asp:TextBox ID="txt_arama2" CssClass="myClass" runat="server" Width="60%"></asp:TextBox>    

  window.onload = function() {
  document.getElementsByClassName("myClass")[0].focus();
  };
dns_nx
  • 3,651
  • 4
  • 37
  • 66
  • Thank you for help. But it does not work by this way. On the page, Textbox is not focused. – Mrs.Cengaver Oct 27 '17 at 10:31
  • Sorry, I forgot one thing. `document.getElementsByClassName` method returns an array of DOM elements. So you just have to select the first by adding `[0]` after the method. Please see my updated code. – dns_nx Oct 27 '17 at 12:44
  • You are right, it works due to changing them. Actually my problem is : if you are entered on PC browser, textbox is focused. But if you are entered by using Ipad browser, textbox is not focused. I do not understand it. Do you have any idea about this subject ? – Mrs.Cengaver Oct 27 '17 at 13:38
  • I don't have any apple products so I cannot tell you why this is not working. But a quick search about "ipad textbox focus javascript" gave me a lot of links regarding this issue. This one is probably your solution, but it is using jquery instead of native javascript. Maybe it helps you to solve you issue. https://stackoverflow.com/questions/30752250/ios-workaround-for-manually-focusing-on-an-input-textarea – dns_nx Oct 28 '17 at 11:55