Hi All,
I'm pretty much a novice at javascript and have a hit a roadblock with a piece of code. I have a form which automatically adds new fields when the user tabs off of a certain field. This part of the code works fine, however when I try to set the options for a newly created 'select' box, I receive an error that the form element is null or not an object. As you can see in the code below, I have tried debugging the code by alerting all of the form values to screen. The name of the newly created 'select' element appears so to my understanding it is now a valid form element. I have also tried checking to see if it is a
null value and it skips through this check which indicates to me that it is neither a null value nor an invalid object. So can anyone see anything glaringly obvious in the code below (apart from the fact that it probably isn't the tidiest code ever written)?
<SCRIPT LANGUAGE="JavaScript">
<!--
function createResource(val)
{
var departmentVal = val.value;
var lineID = document.updateCar.elements ["form_count"].value;
var resourceID = 'frmResource' + lineID;
var objOption = document.createElement("option");
var cnt;
cnt = 0;
for(var i=0;i<document.updateCar.elements.length;i++)
{
// alert(document.updateCar.elements.name + ' **' +
document.updateCar.elements.value + '**');
}
//alert(document.updateCar.elements["frmResource" + lineID].value);
if (document.updateCar.elements["frmResource" + lineID] == '')
{
alert('the value is null');
}
document.updateCar.elements["frmResource" +
lineID].options[cnt]=new Option('Select A Resource', 'X', false,
false);
document.updateCar.elements["frmResource" +
lineID].options.length=0
}
//-->
</script>
The line that errors out is:
if (document.updateCar.elements["frmResource" + lineID] == '')
Cheers
ab
I'm pretty much a novice at javascript and have a hit a roadblock with a piece of code. I have a form which automatically adds new fields when the user tabs off of a certain field. This part of the code works fine, however when I try to set the options for a newly created 'select' box, I receive an error that the form element is null or not an object. As you can see in the code below, I have tried debugging the code by alerting all of the form values to screen. The name of the newly created 'select' element appears so to my understanding it is now a valid form element. I have also tried checking to see if it is a
null value and it skips through this check which indicates to me that it is neither a null value nor an invalid object. So can anyone see anything glaringly obvious in the code below (apart from the fact that it probably isn't the tidiest code ever written)?
<SCRIPT LANGUAGE="JavaScript">
<!--
function createResource(val)
{
var departmentVal = val.value;
var lineID = document.updateCar.elements ["form_count"].value;
var resourceID = 'frmResource' + lineID;
var objOption = document.createElement("option");
var cnt;
cnt = 0;
for(var i=0;i<document.updateCar.elements.length;i++)
{
// alert(document.updateCar.elements.name + ' **' +
document.updateCar.elements.value + '**');
}
//alert(document.updateCar.elements["frmResource" + lineID].value);
if (document.updateCar.elements["frmResource" + lineID] == '')
{
alert('the value is null');
}
document.updateCar.elements["frmResource" +
lineID].options[cnt]=new Option('Select A Resource', 'X', false,
false);
document.updateCar.elements["frmResource" +
lineID].options.length=0
}
//-->
</script>
The line that errors out is:
if (document.updateCar.elements["frmResource" + lineID] == '')
Cheers
ab