1

I am doing a jQuery focus on a text field. In Chrome and IE the cursor starts after the value but in firefox it starts befoe the value. How do I make it so it starts after the value in firefox?

Relevant code below:

$($.cookie("inputFocus")).focus()

Whole jQuery code below:

<script>
$(document).ready(function() {
    $("#state").change(function () {
    this.form.submit();
})
var focus = $.cookie("inputFocus");
$(focus).focus();
$(focus)[0].setSelectionRange($(focus).val().length, $(focus).val().length);

$("#supplier_name").val($("#supplier_name").val());
$("#aircraft_type").val($("#aircraft_type").val());
var typingTimer;                
var doneTypingInterval = 600;  

$('#supplier_name').keyup(function(){
    clearTimeout(typingTimer);
    if ($('#supplier_name').val) {
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    }
    $.cookie("inputFocus", "#supplier_name"); 
});

$('#aircraft_type').keyup(function(){
    clearTimeout(typingTimer);
    if ($('#aircraft_type').val) {
        typingTimer = setTimeout(doneTyping, doneTypingInterval);
    }
    $.cookie("inputFocus", "#aircraft_type"); });
function GetQueryStringParams(sParam)
{
    var sPageURL = window.location.search.substring(1);
    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++)
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam)
        {
            return sParameterName[1];
        }
    }
}

var state = GetQueryStringParams('state');
var supplier_name = GetQueryStringParams('supplier_name');
var aircraft_type = GetQueryStringParams('aircraft_type');

    if(supplier_name === "" && state === "any" && aircraft_type === "") {
            $('#clear').attr('disabled','disabled');
    }
    $("#clear").click(function() {
    if(state === "any") {
        $("#aircraft_type").val("");
        $("#supplier_name").val("");
    } else {
        $('#state option:selected').remove();
        $("#aircraft_type").val("");
        $("#supplier_name").val("");    
    }
    });

function doneTyping () {
    $("form").submit();
}

});
</script>
sephiith
  • 1,187
  • 3
  • 23
  • 47
  • There is no jQuery solution. http://stackoverflow.com/questions/499126/jquery-set-cursor-position-in-text-area take a look at Marks solution – SwiftMango Jan 31 '13 at 03:37
  • Maybe you missing ';' in somewhere (ex: line 4), so ff cannot understand your code. Correct all mistake and try again ^.^ – Stiger Jan 31 '13 at 03:46
  • @Stiger: you can ignore ; in javascript – phnkha Jan 31 '13 at 03:51

1 Answers1

5

You can use setSelectionRange to set the caret position.

Try my demo here:

$('input').focus();
$('input')[0].setSelectionRange($('input').val().length, $('input').val().length);
phnkha
  • 7,782
  • 2
  • 24
  • 31
  • 1
    This seems to work the best cross-browser. Here's a slightly more reusable version of the same thing: http://jsfiddle.net/ChrisMBarr/Jc9N8/3/ – Chris Barr Jan 31 '13 at 03:44
  • I have inserted that. Now I get the following error: TypeError: $(...)[0] is undefined [Break On This Error] $(focus)[0].setSelectionRange($(focus).val().length, $(focus).val().length); – sephiith Feb 03 '13 at 22:27