Try making the following changes:
function calc(){
// Only changed one as an example...youd need to change them all accordingly
one = document.getElementById(edit-submitted-food).value;
two = document.webform-client-form.submitted[timber].value;
three = document.webform-client-form.submitted[flood_protection].value;
four = document.webform-client-form.submitted[soil_protection].value;
five = document.webform-client-form.submitted[carbon_storage].value;
six = document.webform-client-form.submitted[recreation].value;
seven = document.webform-client-form.submitted[aesthetics].value;
eight = document.webform-client-form.submitted[cultural_heritage].value;
//Need to change id-of-the-box-that-you-want-to-contain-the-total as appropriate
document.getElementById(id-of-the-box-that-you-want-to-contain-the-total).value = (one * 1) + (two * 1) + (three * 1) + (four * 1) + (five * 1) + (six * 1) + (seven * 1) + (eight * 1);
}
Cheers, will give it a go tomorrow when Im back in the office :D
also, why are you * 1 in the sum? Cant see a reason for it.
Zypyder :
Best tip if your using javascript - Use a framework - jQuery.
You will use less code and have more abilities which are easier to implement.
A quick example of me copying data from one field from another where Later I used the function when changes etc were done in another function..
function copyAddress()
{
if ($("input#BillingAddress").val() == "") {
$("input#BillingAddress").val($("input#ShippingAddress").val());
}
if ($("input#BillingCity").val() == "") {
$("input#BillingCity").val($("input#ShippingCity").val());
}
if ($("input#BillingState").val() == "") {
$("input#BillingState").val($("input#ShippingState").val());
}
if ($("input#BillingZip").val() == "") {
$("input#BillingZip").val($("input#ShippingZip").val());
}
var billIndex = $("#ShippingCountry option").index($("#ShippingCountry option:selected"))
$("#BillingCountry option:eq("+billIndex+")").attr("selected", "selected")
}
Wont mean a lot but the selector control etc you have is not only more efficient but a lot easier. If you plan to do more coding from now on and use javascript I highly recommend you take up learning jQuery and use of the plugins available.
also, why are you * 1 in the sum? Cant see a reason for it.
Im guessing as it will force the JavaScript engine to cast the variable as a number. Using parseFloat would be better really.
also, why are you * 1 in the sum? Cant see a reason for it.
Im guessing as it will force the JavaScript engine to cast the variable as a number. Using parseFloat would be better really.
Another reason to use a framework.
The thing to remember is...I have no experience in coding beyond a bit of basic html. The code pasted is basically the first bit of code I found which was easy enough for me to understand...ish. Id use jquery if I actually knew wtf it was and that itd work in the system without too much hassle ><
Sadly the getelementbyid didnt work. I think I am limited somewhat in what I can do without modifying whatever template file is responsible for the webform. Im looking into the validation settings to see if I can get it to sum the text boxes on submit and prompt the user if they dont add up to 100.
also, why are you * 1 in the sum? Cant see a reason for it.
Im guessing as it will force the JavaScript engine to cast the variable as a number. Using parseFloat would be better really.
Wrong terminology, you dont cast to convert ;)
also, why are you * 1 in the sum? Cant see a reason for it.
Im guessing as it will force the JavaScript engine to cast the variable as a number. Using parseFloat would be better really.
Wrong terminology, you dont cast to convert ;)
Maybe. Using parseFloat is definitely converting rather than casting. I don’t think if doing something like appending * 1 in a loosely typed language like JavaScript is clear cut either way. I’d call it casting, others might not.