Reputation: 42753
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
Reputation: 74420
Maybe try this:
$("#button").on("click", function () {
$('#inp').focus().val($('#inp').val());
});
Upvotes: 3
Reputation: 5332
$("#button").on("click", function () {
$("#inp")[0].selectionStart = $("#inp")[0].selectionEnd = $('#inp').val().length;
});
Upvotes: 1