Connecting Tech Pros Worldwide Help | Site Map

How to Capture onFocus event

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 23rd, 2005, 10:41 AM
Paresh Shah
Guest
 
Posts: n/a
Default How to Capture onFocus event

Hi Friends...


I have an query on how to capture onFocus event for all the input
controls without writing onFocus event in the <input> tag.

say my html form has 3 or 4 or 5 input text boxes, now I want to
capture onFocus event for all the text boxes without writing onFocus
code in <input> tag.

for example:
<form name="frm1">
<input type="text" name="t1">
<input type="text" name="t2">
<input type="text" name="t3">
</form>

now in above example whenever user shifts the focus to input 1 i.e. t1
box, then some action should be taken say alert message is displayed,
then when focus shifts to input 2 i.e. t2 box, then same action is
displayed & alert message is displayed. for this I dont want to write
code given below:

<form name="frm1">
<input type="text" name="t1" onFocus="alert('hi')">
<input type="text" name="t2" onFocus="alert('hi')">
<input type="text" name="t3" onFocus="alert('hi')">
</form>



If some body could help me.....

Thanks in Advance

Regards
Paresh Shah

  #2  
Old July 23rd, 2005, 10:41 AM
Berislav Lopac
Guest
 
Posts: n/a
Default Re: How to Capture onFocus event

Paresh Shah wrote:[color=blue]
> Hi Friends...
>
>
> I have an query on how to capture onFocus event for all the input
> controls without writing onFocus event in the <input> tag.[/color]

var inputs = frm1.getElementsByTagname('input');
for(var i=0; i < inputs.length; i++) b
inputs[i].onfocus = function {
alert('hi');
}
}

or

var inputs = frm1.getElementsByTagname('input');
inputs[0].prototype.onfocus = function {
alert('hi');
}


I didn't test those, and I might be a bot off with the syntax, but this is
the direction to look at.

Berislav

--
If the Internet is a Marx Brothers movie, and Web, e-mail, and IRC are
Groucho, Chico, and Harpo, then Usenet is Zeppo.


  #3  
Old July 23rd, 2005, 10:42 AM
Michael Winter
Guest
 
Posts: n/a
Default Re: How to Capture onFocus event

On Tue, 4 May 2004 13:11:16 +0200, Berislav Lopac
<berislav.lopac@dimedia.hr> wrote:

[snip]
[color=blue]
> var inputs = frm1.getElementsByTagname('input');[/color]

var inputs = form.getElementsByTagName( 'INPUT' );
[color=blue]
> for(var i=0; i < inputs.length; i++) b[/color]

for( var i = 0, n = inputs.length; i < n; ++i ) {
[color=blue]
> inputs[i].onfocus = function {
> alert('hi');
> }[/color]

It would be best to use a single function and assign its reference, rather
than create an anonymous function for each element.
[color=blue]
> }
>
> or
>
> var inputs = frm1.getElementsByTagname('input');[/color]

As above.
[color=blue]
> inputs[0].prototype.onfocus = function {[/color]

I don't know how successful that will be. Some browsers may not provide a
prototype for the element. The single function argument above applies
here, too.
[color=blue]
> alert('hi');
> }
>
> I didn't test those, and I might be a bot off with the syntax, but this
> is the direction to look at.[/color]

It's a shame that IE is such a pile of crap. If it supported event
capturing (as introduced in DOM 2 Events), this would be so much easier.

Mike

--
Michael Winter
M.Winter@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.