How do I change textarea value and put cusor at the beginnig?
This is the code:
<html>
<head>
<script src="jquery-2.0.0.js" type="text/javascript"></script>
<script>
function testfn () {
$('.texta').html('stuff');
if ($('.texta').createTextRange) {
var part = $('.texta').createTextRange();
part.move("character", 0);
part.select();
} else if ($('.texta').setSelectionRange) {
$('.texta').setSelectionRange(0, 0);
}
$('.texta').focus();
}
</script>
</head>
<body>
<textarea class="texta"></textarea>
<button onclick="testfn();">test</button>
</body>
</html>
After the button is pressed, textarea value is changing and it's focused.
But the cursor is at the end of the text. How do I move it to the
beginning of this textarea after its value has been changed?
UPD: @San's approach works well in Chrome, I still need a solution for FF
No comments:
Post a Comment