faqts : Computers : Programming : Languages : JavaScript : Forms : TextAreas/TextFields

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 85 people (9%) answered Yes
Recently 1 of 10 people (10%) answered Yes

Entry

How do I find out the INPUT Name on my form, if it is dynamically generated?

May 16th, 2000 19:42
Beckham Lexon,


----Solution-----
Finally solved! Identify the element name by its distinctive 
characteristics (e.g. longer names, consistent naming conventions, 
etc). Grabbing by that characteristics, loop through the forms.element 
and search for that specific field. Then, return its name.

----Illustration----
To illustrate, on submitting a form that accepts a picture file as 
attachment, checks on the attachment for the file format. The text 
field name is auto-generated in this scenario:

----------- OnSubmit event handler-----------
for (var e = 0; e < document.forms[0].elements.length; e++) {
	var field = document.forms[0].elements[e];
	if (field.type.toLowerCase() == 'file'){
		if (field.value == "") {
			alert("Picture must be attached.")
			field.focus();
			return false;
		} else 
			return check(field.value);
		e = document.forms[0].elements.length;
	}
}

----------- In the HTML Header -----------
function check(filename) {
	var end = filename.length;
	var start = end - 4;
	var result = filename.substring(start, end);
	if (result != ".gif" && result != ".jpg") {
		alert("Filename MUST end with \".gif\" or 
\".jpg\"\nPlease select another file.");
		return false;
	} else 
		return true;
}