How to Capture onFocus event

Discussion in 'Javascript' started by Paresh Shah, May 4, 2004.

  1. Paresh Shah

    Paresh Shah Guest

    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
     
    Paresh Shah, May 4, 2004
    #1
    1. Advertising

  2. Paresh Shah wrote:
    > 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.


    var inputs = frm1.getElementsByTagname('input');
    for(var i=0; i < inputs.length; i++) b
    inputs.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.
     
    Berislav Lopac, May 4, 2004
    #2
    1. Advertising

  3. On Tue, 4 May 2004 13:11:16 +0200, Berislav Lopac
    <> wrote:

    [snip]

    > var inputs = frm1.getElementsByTagname('input');


    var inputs = form.getElementsByTagName( 'INPUT' );

    > for(var i=0; i < inputs.length; i++) b


    for( var i = 0, n = inputs.length; i < n; ++i ) {

    > inputs.onfocus = function {
    > alert('hi');
    > }


    It would be best to use a single function and assign its reference, rather
    than create an anonymous function for each element.

    > }
    >
    > or
    >
    > var inputs = frm1.getElementsByTagname('input');


    As above.

    > inputs[0].prototype.onfocus = function {


    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.

    > 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.


    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
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, May 4, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?SSBhbSBTYW0=?=

    onFocus Event Refresh

    =?Utf-8?B?SSBhbSBTYW0=?=, Apr 21, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    3,187
    Jeebu
    Apr 21, 2005
  2. Scott Blood

    Custom ListControl OnFocus Event not working

    Scott Blood, Jul 31, 2006, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    172
    Scott Blood
    Jul 31, 2006
  3. David Perez

    textbox onfocus event

    David Perez, Apr 21, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    206
    Ken Cox [Microsoft MVP]
    Apr 21, 2005
  4. sybmathics

    onfocus event for hyperlinks

    sybmathics, Oct 8, 2005, in forum: Javascript
    Replies:
    11
    Views:
    188
    Thomas 'PointedEars' Lahn
    Oct 16, 2005
  5. Confused

    onfocus event on Firefox

    Confused, Jul 1, 2008, in forum: Javascript
    Replies:
    1
    Views:
    85
    Bjoern Hoehrmann
    Jul 1, 2008
Loading...

Share This Page