PDA

View Full Version : Referencing Field with JS


momal_1
11-24-2004, 04:41 AM
Hi,

I have a function which references a form element where the name can change.

Here is what it is so far:


function test***ntion(fieldName) {

document.form1.fieldName.value=='Hello'
}


I have tried the getElementID() method, but that doesnt work. As the field doesnt physically exist, i always get the error sayin


"document.form1.fieldName.value is null or not an object"


Can snyone help?

Thanks

jbbrwcky
11-24-2004, 10:01 AM
A couple of things:

You need to change the call to the function; and also you were not setting the value, but rather using a comparison (this is, I think, why you were getting that error). Next, even if the name changes, you can use a class right?


In the script:
<script type="text/javascript">
function testfunction(obj) {
for (var i=0; i<obj.elements; i++) {
if (obj.elements[i].className == "variableClass") {
var field = obj.elements[i];
break;
}
}
field.value='Hello';
return true;
}
</script>


Then change the form, add in the classname to your form element (assuming you can).


<form name="fooform" onsubmit="return testfunction(this)" method="post">
<input type="text" class="variableClass" name="changingName" />
</form>


Jbbrwcky