How to automatically setfocus on text input in a and go back to same page after data has been entered..
Just like after you enter a data in input box, the form will refresh and the focus will go in the textbox again..
thanks in advance..
How to automatically setfocus on text input in a and go back to same page after data has been entered..
Just like after you enter a data in input box, the form will refresh and the focus will go in the textbox again..
thanks in advance..
As correctly pointed out, JavaScript is the usual way to restore focus, but there are a few practical patterns depending on whether the form submission reloads the page or you submit via AJAX.
For a simple, no-script fallback use the HTML5 autofocus attribute on the input. It works in modern browsers and requires no script:
<input id="myInput" name="myInput" autofocus> See the spec and notes about accessibility and browser support: HTML5 autofocus.
If the page reloads after the form POST, use the Post/Redirect/Get pattern and include an anchor for the field so the browser jumps to it. In PHP you can redirect back to the same page with a fragment:
header('Location: ' . $_SERVER['PHP_SELF'] . '#myInput');
exit; Some browsers will scroll to the anchor but not always set keyboard focus. To be certain, add a small script that runs on load and focuses the element by id:
window.addEventListener('DOMContentLoaded', function () {
var el = document.getElementById('myInput');
if (el) el.focus();
}); This uses the standard focus API; details at HTMLElement.focus().
If the form is submitted via AJAX, keep the page intact and call focus after you update the UI — that is usually the simplest UX. Also avoid older form-name shortcuts and prefer id + getElementById or querySelector for reliable targeting. Finally, be mindful of accessibility: autofocus can confuse screen-reader users or keyboard-only users, so use it only where it improves workflow.
Jump to Post— chintan@dani 0You can achieve this using javascript.
suppose your for name is frm and 'input' name is txtbox.
you can set using
document.frm.txtbox.focus();
You can achieve this using javascript.
suppose your for name is frm and 'input' name is txtbox.
you can set using
document.frm.txtbox.focus();
okay.. i got it.. thanks a lot..
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.