Every time I refresh (using timeout) the page, the input value stays the same but the cursor jumps to the start of the value.
I've tried using .click()
instead of .focus()
it doesn't achieve the results I'd like.
I've added autofocus
to the element but that didn't help either.
EDIT: I've noticed this seems to be an issue with Google Chrome browser, I've tried it with Safari and it worked as intended. If anyone has any suggestions to make it work on Chrome, it would be much appreciated.
HTML tags
<div class="searchContainer">
<h4>Find your favourite songs, artist or albums.</h4>
<input type="text" id="searchBar" class="searchBar" autofocus value="<?php echo $query; ?>" onfocus="this.value = this.value;" placeholder="Enter your search here">
</div>
JavaScript
<script>
$(".searchBar").focus();
$(function() {
// refreshes page on each time user types a word after 2000ms (2s)
var refresh;
$(".searchBar").keyup(function() {
clearTimeout(refresh);
//restarts refresher to wait for user to finish typing
refresh = setTimeout(function() {
var value = $(".searchBar").val();
changePage('search.php?query=' + value);
}, 2000);
})
})
</script>
I'd like the value to show in the input bar after each refresh, but the cursor to stay at the end of the input so the user can continue typing from where they left off.
(How usual search bars work where each input value is refreshed so that it displays results of whats in input instantly but if the user makes a mistake they can continue to remove characters from where they left off).
If anyone has any ideas, they would be very welcome. Thanks :)