Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- Variable scope and functions


le_gber - 3:11 pm on May 8, 2009 (gmt 0)


Hi all,

The goal: I have a form with different default fields values (name, surname, email). I want to empty the field onfocus and put the default value back onblur if the field is empty.

I am trying to be clever and do the whole form in one js function (I know how to do it if I create a function for each individual field).

I've got an issue on the following:

window.onload = function(){
if(document.getElementById('myform')){
ffields = document.getElementById('myform').getElementsByTagName('input');
for(f in ffields){
ffields[i].onclick = function(){ alert(i); }
}
}
}

at the moment I get 'namedItem' in my alert box. I tried

ffields[i].onclick = function(){ alert(i); }

but that doesn't work either it alerts '[object MouseEvent]'

Basically I need somebody to tell me how to pass 'i' inside the function, and I'd be forever grateful to them - I've been trying to do this for the entire afternoon :(

I promise to share the finished file :)


Thread source:: http://www.webmasterworld.com/javascript/3909506.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com