0

See bellow code Here is HTML

 <asp:TextBox ID="txtInTime" runat="server" CssClass="TextBox" Width="60px"
    OnClick="fnFocus(this.id)" ></asp:TextBox>

here is javascript

   function fnFocus(id) {
   document.getElementById(id).focus();    
   }

Above code working in all browser expect chrome... I want textbox click to focus of textbox at first position..In chrome not comming in first position of character.. please help... Any Suggestion...??

Boss
  • 445
  • 2
  • 8
  • 24

2 Answers2

3

You can do this using setSelectionRange:

function fnFocus(id) {
    var input = document.getElementById(id);
    input.focus();
    input.setSelectionRange(0, 0);
}

Demo: Fiddle

palaѕн
  • 72,112
  • 17
  • 116
  • 136
  • Its working in Chrome.... but it effects in IE. In IE its working for 1st click, but if you click 2nd time its not working – Boss Oct 30 '13 at 12:55
0

setSelecionRange is not working for me in Chrome.

Try this X-Browser:

//Function to set the cursor position and then we set the position for your textbox

$.fn.setCursorPosition = function(pos) {this.each(function(index, elem) {
if (elem.setSelectionRange) {
  elem.setSelectionRange(pos, pos);
} else if (elem.createTextRange) {
  var range = elem.createTextRange();
  range.collapse(true);
  range.moveEnd('character', pos);
  range.moveStart('character', pos);
  range.select();
}   });   return this; };

$('#txtInTime').setCursorPosition(0);

// In your code

<asp:TextBox ID="txtInTime"...... OnClick="$(this).setCursorPosition(0);"></asp:TextBox>

Source

seba47
  • 320
  • 1
  • 11
  • sure? the "$('#txtInTime').setCursorPosition(0);" should be on the onclick attribute like : – seba47 Oct 30 '13 at 13:08
  • sorry...continue here....If you fires the function in ONCLICK of the field, it would be something like that: – seba47 Oct 30 '13 at 13:09