Click to See Complete Forum and Search --> : GetFocus???


Lion
01-08-2003, 12:05 PM
Hi @all,
I want to know where the cursor is in an HTML-form. There are several textareas on the page and I need to find out via JS in which of these the user is working currently. So how do I find out which is the "focused" textarea? Is there something like GetFocus();???

gil davis
01-08-2003, 12:23 PM
Is there something like GetFocus();???
No.

You would have to add an onfocus event handler to each textarea and keep track of which one gets focus in some global variable.

<script>
var which = null;
</script>
...
<form ...>
<textarea name="ta1" onfocus="which=this" onblur="which=null"></textarea>
<textarea name="ta2" onfocus="which=this" onblur="which=null"></textarea>
<textarea name="ta3" onfocus="which=this" onblur="which=null"></textarea>
</form>

The variable "which" will contain either null or a pointer to the textarea that currently has focus.

fonio
04-21-2009, 05:44 PM
first i wanted to say that this topic is 3rd item for the google search with " javascript getfocus" (in France), so i'm happy if my post quickly help you as i like to be when using the web.

so what i found,

into the html :

<body onfocusin="get_focus();">
.../...
<input id="HiddenFocusOn" type="hidden" />


into the js code : (only ie, for the moment) :

function get_focus(){
var e=window.event;
document.getElementById('HiddenFocusOn').value=e.srcElement.id;
}

=> each time you click a control, its id goes in the HiddenFocusOn.value