events - what can and can't be done when they fire

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
krraleigh
Forum Commoner
Posts: 86
Joined: Tue Jul 17, 2007 2:52 pm

events - what can and can't be done when they fire

Post by krraleigh »

I have a simple form that contains one text field:

<input name="fName" type="text" id="fName"
maxlength="25" value="<?php echo $_SESSION['SES_fName']; ?>"
onblur="myObj.setValues('f1', this.value, name)"/>

When I click into the cell of this text box is an event fired?
If it is what kind of information can I retrieve from this event?
Can I obtain the textboxes id, the name of the field, any of the attributes?

I am trying to find a way to automate how I capture information for forms validation
Right now I have everything hard coded and it takes alot of work and maintenance.

PS I am pretty sure that jquery, YUI, and the likes would make life pretty easy,
but the learning curve here is just as important as getting the job done.
In other words I need to find the limitations of what can and can't be done.

any insight would be greatly appreciated
Kevin
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: events - what can and can't be done when they fire

Post by kaszu »

When I click into the cell of this text box is an event fired?
In event listener function 'this' keyword will reference an input, so you can get any attribute (id, name, value, etc.) of the input.

Example (won't work in IE):

Code: Select all

var node = document.getElementById('fName');
node.addEventListener('blur', function () {
    alert(this.name);
    alert(this.value);
    alert(this.id);
}, false);
krraleigh
Forum Commoner
Posts: 86
Joined: Tue Jul 17, 2007 2:52 pm

Re: events - what can and can't be done when they fire

Post by krraleigh »

This is exactly what I was looking for

Now is there a reason that I should not take all of the work out of the form?

Right now I use blur to call a function to set my data fields. onblur=setMyDataFields(this.name,this.value,this.id);
I would like to automate everything so that all I have to do is include my js script

So what kind of trouble would I get myself into if I automated everything?
Is there a graceful way of checking to see if what I want to do is available so that I can use a try catch block if everything falls apart?

thanx
Kevin
krraleigh
Forum Commoner
Posts: 86
Joined: Tue Jul 17, 2007 2:52 pm

Re: events - what can and can't be done when they fire

Post by krraleigh »

Well I thought I had found my silver bullet:
var node = document.getElementById('fName');
node.addEventListener('blur', function () {
alert(this.name);
alert(this.value);
alert(this.id);
}, false);
,

but IE won't play nice
Is there anyway to capture the onblur event when a user leaves a textinput field
in an IE browser? I can capture the CLICK event, but that is only one that I have
been able to capture thus far.

I've tried everything that I could find out there and I have hit a brick wall
Spent hours going over the MSDN's site and couldn't find anything there either

The closest thing that I could find is window.event, but it fails in IE 8
I don't have earlier versions of IE to test on YET

Can anyone help me solve this mistery?

thanx
Kevin
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: events - what can and can't be done when they fire

Post by VladSun »

Maybe:
[js]var node = document.getElementById('fName');node.onblur = function () {   alert(this.name);   alert(this.value);   alert(this.id);}[/js]
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
kaszu
Forum Regular
Posts: 749
Joined: Wed Jul 19, 2006 7:29 am

Re: events - what can and can't be done when they fire

Post by kaszu »

As I wrote it won't work in IE, because it doesn't support standard even registration.
For cross-browser solution see http://www.quirksmode.org/js/events_advanced.html
Post Reply