1

How to Get the text element (or its id) in which there is caret?

SMUsamaShah
  • 7,310
  • 21
  • 87
  • 125
  • possible duplicate of [How to determine which html page element has focus?](http://stackoverflow.com/questions/483741/how-to-determine-which-html-page-element-has-focus) – nc3b Mar 11 '11 at 08:03

4 Answers4

3

Edit: Guess, I completely misunderstood the word "caret" as it stands for "cursor" in this context... not a native speaker. In case someone looks for a way to find the input which has the "^" character in its value:

Try this plain JavaScript code on jsfiddle:

var inputs = document.getElementsByTagName("input");
var ids = [];
for(var i = 0; i < inputs.length; i++) {
    if(inputs[i].value.match(/.*\^.*/)) {
      ids.push(inputs[i].id);
    }
}

It goes through all "input" elements and filters out those with a caret somewhere in their value. "ids" holds those ids.

Wolfram
  • 7,989
  • 3
  • 43
  • 64
3

document.activeElement will give you this in most browsers.

Tim Down
  • 306,503
  • 71
  • 443
  • 520
1

if you mean how to get text box having focus the answer is you can't.

You could script to "onfocus" event, and record the last-focused field in a variable somewhere. You also have to use "onblur" event to clear last-focused field variable.

HTH

Ivo Stoykov

i100
  • 4,313
  • 1
  • 20
  • 20
0

For example:

var elementWithFocus;

function updateFocus(callingElement)
{
    elementWithFocus = callingElement;
}

Then in your inputs:

<input onfocus="updateFocus(this);" onblur="updateFocus(null);"></input>

Play around with it on jsfidle.

Zsub
  • 1,739
  • 2
  • 14
  • 28