calculate value based on fields

# Thread: calculate value based on fields

1. Senior Member
Join Date
Dec 1969
Posts
468

## calculate value based on fields

I&#039;m trying to do is calculate total based on values of some fields.<BR><BR>&#060;input type=radio name=fee value=325.00&#062;325<BR>&#060;input type=radio name=fee value=425.00&#062;425<BR><BR>&#060;input type=radio name=rental value=35.00&#062;35<BR>&#060;input type=radio name=rantal value=45.00&#062;45<BR><BR>&#060;input type=text name=salestax readonly&#062; - this should be calculated when user selects above option.<BR><BR>Then total should be calculated<BR>&#060;input type=text name=total&#062;<BR><BR>I&#039;m confused as to how can I get value if there is checkbox and how to change the javascript code when there will be dropdown menu.<BR><BR>Any help/pointer will be greatly appreciated, thanks in advance<BR><BR><BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

## Need functions to get value...

...of the various kinds of fields.<BR><BR>&#060;SCRIPT&#062;<BR>function radioGroupValue( group )<BR>{<BR> for ( var ix = 0; ix &#060; group.length; ++ix )<BR> {<BR> if( group[ix].checked ) return group[ix].value;<BR> }<BR> return null; // nothing checked<BR>}<BR><BR>function selectValue( sel )<BR>{<BR> var val = sel.options[sel.selectedIndex].value;<BR> if ( val == null ) val = sel.options[sel.selectedIndex].text;<BR> return val;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>and then you write a master recalc function:<BR><BR>&#060;SCRIPT&#062;<BR>function recalcTotal( frm )<BR>{<BR> var fee = parseFloat( radioGroupValue( frm.fee ) );<BR> var rental = parseFloat( radioGroupValue( frm.rental ) );<BR> var taxrate = parseFloat( selectValue( frm.taxrate ) );<BR> var total = taxrate * ( fee + rental );<BR> frm.total.value = total;<BR>}<BR>&#060;/SCRIPT&#062;<BR><BR>[Above just a demo; adjust as needed.]<BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•