Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get currently focussed field 2

Status
Not open for further replies.

rhowes

IS-IT--Management
Jun 6, 2001
140
ZA
Hi All

Is there a simple way to get a referrence to the form field that currently has focus?

I want to implement copy and paste functionality and I need to know the Id of the currently focussed field. Once I have that then this works:

var fieldId = "text1";
var ptr = document.getElementById(fieldId);
var paste = ptr.createTextRange().execCommand('Paste');

I want to get the Id of the currently focussed field instead of specifying it as I have above. I know I can set a variable every time a field gets focus but I was wondering if there is a simpler/more elegant way.

Thanks
Cheers

Richard
(Johannesburg, Shouth Africa).
 
The follwing code works in IE not in Mozilla because thisone doesn't have an ActiveElement property of the document object:

<input type=text id=txt>
<input type=text id=txt2>
<script>
document.getElementById('txt2').focus();
document.activeElement.value = &quot;this box got the focus&quot;;


// here is a handy piece of script to get all the properties, events and methods of the document object:
var obj = document
for(hh in obj) {
document.write(hh + &quot;<br>&quot;);
}

// want do do the same with the txtbox:
var obj = document.getElementById('txt')
for(hh in obj) {
document.write(hh + &quot;<br>&quot;);
}

</script>
 
Those links to get the properties of an element are invaluable to me!
Have a star!
 
Thanks very much (another star). I thought something to give you the &quot;activeElement&quot; would be a logical thing for a DOM to have.

Pity it's only IE (like many useful things I have to admit).

Cheers
Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top