Field.length returns undefined

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suchismitaswain
    New Member
    • Aug 2007
    • 1

    Field.length returns undefined

    Hi,
    I have a field in jsp page that has some textboxes. but the texboxes are dynamically created depending on the no of rows in a table.
    When i click the update button in my jsp page it has to get the values in the text boxes and get the total.
    My java script first checks if the field is valid i.e if there are any textbox in the field. If there are no fields then it set a variable as false. but if there are any fields it takes the length of the field to determine the no of textboxes and iterates through each text box to add all the value. The javascript works fine if there are no textboxs or if there are more then one textbox..
    But when i have only one textbox in the field the field.length command returns me the value 'undefined'.
    Below is the part of my java script code.

    [CODE=javascript]function validateSupplie rCost()
    {
    var bValid = false;
    if(document.dci sForm.txtTotalC ompEstimate){
    bValid = true;}
    var totalCompEstima te = document.dcisFo rm.txtTotalComp Estimate;
    var supplierCostRes ponseGrp = document.dcisFo rm.rbCost;
    var respInvest = document.dcisFo rm.C5;
    var respDieMod = document.dcisFo rm.C4;
    var respPartMod = document.dcisFo rm.C2;
    var respOther = document.dcisFo rm.C1;
    var bResponseType = false;
    if(respInvest.c hecked == true || respDieMod.chec ked == true || respPartMod.che cked == true|| respOther.check ed == true) {
    bResponseType = true;
    }
    var bSupplierRespon se = false;

    for (var i =0; i< supplierCostRes ponseGrp.length ; i++)
    {
    if (supplierCostRe sponseGrp[i].value == "Y") {
    if (supplierCostRe sponseGrp[i].checked == true) {
    bSupplierRespon se = true;
    }
    }
    }
    if(bSupplierRes ponse == true && bResponseType == true) {
    if(bValid == true){
    var noOfFields = totalCompEstima te.length;
    var totalCost = 0;
    var cost = 0;
    for(i=0; i< noOfFields; i++)
    {
    cost = totalCompEstima te[i].value;
    totalCost = totalCost + cost;
    }
    if(totalCost > 0){
    return true;
    } else {
    return false;
    }
    }else {
    var compEstimate = document.dcisFo rm.txtCompEstim ate;
    if(compEstimate .value >0){
    return true;
    } else {
    return false;}
    }
    } else {
    return true;
    }
    }[/CODE]
    Last edited by gits; Aug 31 '07, 07:51 AM. Reason: added code tags
  • dmjpro
    Top Contributor
    • Jan 2007
    • 2476

    #2
    Welcome to TSDN.

    Glad to see you that you are following Posting Guidelines.
    Anyway, you have a line in JavaScript.

    [code=javascript]
    var supplierCostRes ponseGrp = document.dcisFo rm.rbCost;
    //Changes this line....
    var supplierCostRes ponseGrp = document.getEle mentsByName('rb Cost');
    //Into this one and see the what hapens!
    [/code]

    Best of luck with your code.

    Kind regards,
    Dmjpro.

    Comment

    Working...