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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onBlur on whole form? 2

Status
Not open for further replies.

tomvdduin

Programmer
Sep 29, 2002
155
NL
I want to check textfields inside a form when a user exits that field. I could do it with a onblur on the textfield, but I want all textfields in a form to be checked, so I thought that a onblur on the form element should do the trick, but that doesn't work.

What kind of other options are there?
 
Put an onBlur on each element and you can check the entire form each event (if that's what you really want).

Can you elaborate on what it is that you are trying to do?

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
If you didn't want to actually put an onblur into each element on the form (and as you observed, putting it on the form itself is of no use)... then I would suggest the following as a solution (assuming you want the same onblur code on each element)...

Set some javascript to run onload (in the body tag is a good place for this) that loops through all the elements of the form and appends an onblur to them (programatically).

It's not as elegant as your thoughts of using the form tag, but it'll do the trick and won't weigh down your pages.

Jeff
 
Jeff,

Interesting one...

My javascript knowledge is not good, I'm a php programmer. Can you give me a small piece of code I can start with?
 
<script>
function addEvents(){
theForm = document.getElementById("myForm")
for (x=0; x<theForm.elements.length; x++){
theForm.elements[x].attachEvent("onBlur",myBlurFunction)
}
}

function myBlurFunction(){
myFormField = event.srcElement
//now process the event
}

</script>

<body onLoad="addEvents()">
<form id="myForm">

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
mwolf00,

In Internet Explorer it works perfectly, but when I use it in Mozilla, it does not! I've altered the function addEvents, but I can't get it working in mozilla. Do you know what the problem is?

Tom
 
Tom,

I haven't worked with Mozilla, so I'm not too sure. Are you able to tell if the onBlur even it attached? You could test by saying

function myBlurFunction(){
alert("function called")
}

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
no, the onblur function isn't attached to the fields, but the 'addEvents' functions IS called.
It looks like in your function 'addevents' the line: theForm = document.getElementById("myForm") doesn't work for mozilla. I altered that line into theForm = document.forms[0], but still no luck. Then I added the line: if (theForm == null) return; to it, and it looks like in IE(6) there's a value for the variabele theForm, and in Mozilla it's null.

I think it's kinda strange, perhaps people with experience with mozilla can help?

Tom
 
Does your form definately have an ID (not to be confused with a NAME)?

If so, try changing this:

Code:
theForm = document.getElementById("myForm")

to read this:

Code:
var theForm = document.getElementById('myForm');

It might well be the all-important "var" keyword that is causing this.

Failing that, kick open Mozilla's Javascript console and see what errors are being thrown up.

Hope this helps,
Dan
 
given a form with a "name" of "myForm", here's how to do it cross-browser:
Code:
function addEvents(){
  theForm = document.myForm;
  for (x=0; x < theForm.elements.length; x++){
    theForm.elements[x].onblur = function() { myBlurFunction(this) };
  }
}

function myBlurFunction(myFormField){
  //now process the event
  alert(myFormField.name);
}

=========================================================
-jeff
try { succeed(); } catch(E) { tryAgain(); } finally { rtfm(); }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top