• 0

[javascript] Focus detection


Question

Can anyone tell me if there's a way to detect focus on an object?

I have a form with several textareas and I need to know in which the user is typing.

I can use the onfocus event but how do I transfer the info from that event to a function that will be used later.

Example: post a new message on this forum and click the b button for bold text. This will add the necessary ibcode to the one and only text area. I however have two textareas so which one is used when pressing b?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

you could have a state variable that points to the appropriate instance. eg. onFocus="setField( this );". then have that set a global variable to be used in other functions.

Link to comment
Share on other sites

  • 0

I tried that but it seems the var I set with onfocus is not a global one since I always get an error like 'var not set' in other functions.

How do I make a var global?

function setfield(target) {
var my_global_var = target;
}

This doesn't make my_global_var a global one

Link to comment
Share on other sites

  • 0

i thought you could just define the variable outside of a function code block. It's scope should be high enough then:

var my_global_var =  null;
function setfield( target )
{
    my_global_var = target;
}

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.