473,399 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

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
Jul 23 '05 #1
2 3107
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[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.
Jul 23 '05 #2
On Tue, 4 May 2004 13:11:16 +0200, Berislav Lopac
<be************@dimedia.hr> 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[i].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
M.******@blueyonder.co.invalid (replace ".invalid" with ".uk" to reply)
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Scott Navarre | last post by:
Hi, I have Red Hat 8.0 and have the default Mozilla browser that comes with it. I am programming in javascript and have come across something problematic. Given the following code: <HTML>...
1
by: Rich Morey | last post by:
Hello, I have created a form and assigned each of the form elements to a class in a style sheet. I would like to know if it is possible to add 'OnFocus' and 'OnBlur' events to the style sheet...
1
by: Michael Albanese | last post by:
I am developing an application to handle my compay's OSHA reporting requirements. Some of the input criteria are technical and narowly defined, so I was trying to prvide what i call "Context...
2
by: swathi | last post by:
----------------------------------------------------------------------- A poll associated with this post was created, to vote and see th results, please visit...
1
by: Eric Trav | last post by:
Hello, I am using mozilla and javascript to change the style background color for my select with onfocus() and back to white with onblur(). When i process onfocus(); i have to click on the...
5
by: Max | last post by:
I've got a default value of some text in an input element when a page loads. What I'd like to do is have the value disappear when the user clicks in the input field. I've figured out I can use:...
3
by: Nebulus | last post by:
Hey all. I'm working on some VERY big forms, and the user will easily lose their location if they get distracted - which happens a lot with the amount of phone traffic they get. Anyway, I have a...
2
by: Jonathan N. Little | last post by:
As part of a JavaScript precheck form validation I noticed a problem with trying to return focus to the field with an error. I have setup a demo page. ...
2
by: wolverine | last post by:
Hi All, In Mozilla Firefox, to onblur and onfocus event of each and every html element, the browser itself will attach a native event handler. I mean if you type,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.