Oto Shavadze
Oto Shavadze

Reputation: 42753

Focus in input field, on other event

HTML:

<form>
    <input id="inp" type="text" value="asd"  />
</form>

<div id="button">
    button
</div>

JS:

$("#button").on("click", function () {
    $("#inp").focus();
});

DEMO: http://jsfiddle.net/YgZx8/1/

If click on button, the above code just selects (and not happens focusing) the text in input field. this happens in safari and chrome, correctly works in opera and firefox.

Question: how to make input focusing in chrome and safari also ?

Upvotes: 0

Views: 69

Answers (2)

A. Wolff
A. Wolff

Reputation: 74420

Maybe try this:

http://jsfiddle.net/YgZx8/10/

$("#button").on("click", function () {
   $('#inp').focus().val($('#inp').val());
});

Upvotes: 3

maximkou
maximkou

Reputation: 5332

$("#button").on("click", function () {
    $("#inp")[0].selectionStart = $("#inp")[0].selectionEnd = $('#inp').val().length;
});

Upvotes: 1

Related Questions