How to pass variable into field id javascript -


i'm pretty new stuff - i've got code that's working, not elegant:

function cost(){ var runningcost=0   if (document.getelementbyid('field_280_0').checked==true)     {     runningcost=100;     }   else      {       if (document.getelementbyid('field_281_0').checked==true)       {       runningcost=runningcost+12       }       if (document.getelementbyid('field_282_0').checked==true)       {       runningcost=runningcost+12       }if (document.getelementbyid('field_283_0').checked==true)       {       runningcost=runningcost+12       }if (document.getelementbyid('field_284_0').checked==true)       {       runningcost=runningcost+12       }if (document.getelementbyid('field_285_0').checked==true)       {       runningcost=runningcost+12       }if (document.getelementbyid('field_286_0').checked==true)       {       runningcost=runningcost+12       }if (document.getelementbyid('field_287_0').checked==true)       {       runningcost=runningcost+12       }if (document.getelementbyid('field_288_0').checked==true)       {       runningcost=runningcost+12       }if (document.getelementbyid('field_289_0').checked==true)       {       runningcost=runningcost+12       }if (document.getelementbyid('field_290_0').checked==true)       {       runningcost=runningcost+12       }     }if (document.getelementbyid('field_292_0').checked==true)     {     runningcost=runningcost+55     } document.getelementbyid('[[total cost#id]]').value=runningcost; } 

i trying use loop rid of if lines:

for (var i=281;i<292;i++)     {         var field = i.tostring();                 if (document.getelementbyid('field_'+ field +'_0').checked==true)             {             runningcost=runningcost+12             }     } 

i've tried lots of variations on theme i'm getting stuck getelementbyid null.

what doing wrong? pointers great.

try removing var field = i.tostring();

i tried quick fiddle see if works way, , does.

html:

<div id="field_285_0"></div> 

javascript:

for (var i=281;i<292;i++) {     if(document.getelementbyid('field_'+ +'_0')) {         $('body').append('exsists: '+ );   //<-- added check if statement     }; } 

Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -