3

Here's a sample form:

<form action="#" method="post">
  Name:<br />
  <input type="text" name="name" value="your name" /><br />

  E-mail:<br />
  <input type="text" name="mail" value="your email" /><br />

  <input type="submit" value="Send"> 
</form>

When you tab to a text input, the value gets highlighted. How can it be disabled?

Any help is appreciated!

Mike

Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Mori
  • 8,137
  • 19
  • 63
  • 91
  • 1
    Take a look here se if it helps: http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area – msmafra Apr 21 '11 at 19:18
  • But that is how we like it. Why don't you? – mplungjan Apr 21 '11 at 20:22
  • @tenshimsm, Great! But I just don't know how to include Mark's code in my form embed code so the cursor be placed at the end of the default value. I wonder if you could help me with it. – Mori Apr 22 '11 at 08:20

3 Answers3

2
var $yourInput;

$yourInput = $("#your_input");

setTimeout(function() {
  return $yourInput.selectRange($yourInput.val().length, $yourInput.val().length);
}, 10);

selectRange function you can find here: jQuery Set Cursor Position in Text Area

Community
  • 1
  • 1
1

Hello here is the solution. Dirty one:

html:

<input type="text" id="a" />
<input type="text" id="b" />

Javascript:

$("input").focus(function(){
    if($(this).val() != ""){
        var elm = $(this);
        var val = elm.val();
        setTimeout(function(){elm.val(val);},1);
    }
});

Demo: http://jsfiddle.net/naveed_ahmad/S2UPs/

Naveed Ahmad
  • 3,176
  • 1
  • 15
  • 18
  • Hi Naveed, Thanks for the answer, but it doesn't seem to work in Firefox and Opera. – Mori Apr 21 '11 at 20:09
0

This is working, at least in Firefox. It triggers the "End" key:

$('input[type=text]').bind('focus',function(){
    var e = jQuery.Event("keydown");
     e.which = 35; // # key code for end key
    $("input[type=text]").trigger(e);
    return false;
});
msmafra
  • 1,704
  • 3
  • 21
  • 34
  • I need a cross-browser solution. Would you please let me know how to include Mark's code in my form? Or maybe the solution offered on the following reference:[link]http://stackoverflow.com/questions/1909220/define-cursor-position-in-form-input-field – Mori Apr 22 '11 at 17:26
  • Sorry, I've tried jCaret, jquery.caret changed my code and even doing it I didn't work in other browser than Firefox. – msmafra Apr 26 '11 at 00:27