0

so i have made a simple price quote script with the basic knowledge (very basic) i have taught myself and i would like to add some more stuff to it...

here is the price quote code on jsfiddle

i have check boxes but they have no script attached to them and i would like to add a value to be added to to the total when the checkbox is check...i found a code for a simple checkbox

function initialize() {
    Total = 0;
    totalprice.innerText = Total;
}

function checkoption(checkbox) {
    checknum = parseInt(checkbox.value);
    if (checkbox.checked == true) {
        Total += checknum;
    } else {
        Total -= checknum;
    }
    totalprice.innerText = Total;
}

How do i get the value created from these two functions (it being 0 if no check boxes are selected) and use it in the calculate function...do i parsefloat something? in this example code is the value Total? and lastly do the functions run in order from top to bottom...therefore these two example functions should go on top of function calculate?

ive been trying different things and can seem to find what to do :S....

4
  • You know, you should declare your variables with var otherwise you're polluting the global scope, which is not a good idea... Commented Aug 2, 2012 at 1:50
  • like put var before? is THAT was declaring a variable is? i thought it was something complicated lol and is that why i see that on so many example codes? lol alright that makes sense thank you!!!...when you say global scope...do you mean the webpage? website? or whole internet? Commented Aug 2, 2012 at 1:54
  • Mmm, I think you need to revisit some basic JS concepts. The concept of global scope is not JS specific, it applies to any language that supports scopes. In the case of JS, scopes are functions, and var ensures that the variable will be declared within the scope and not accessible anywhere else. Commented Aug 2, 2012 at 1:56
  • haha revisit??? i havent even visited the first time...i peeked in the windows and took pictures :P....ill probably look for some videos on youtube or something :D Commented Aug 2, 2012 at 1:57

1 Answer 1

1

The value you want is in the Total variable, which is global (a discouraged practice btw). The function declaration order doesn't matter as long as it's not called before it's definition.

Sign up to request clarification or add additional context in comments.

2 Comments

That's not entirely true, in this case actually the function can be called anywhere. That will be true only if the function has been assigned to a variable. var f = function () {}
thank you so much thats what i needed to know...and im guessing the initialize function will make sure there is always a value for Total? (eg 0 if nothing is check) and i can use that in my function below?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.