How do I Use a variable multiple times in different functions












0















I am attempting to make a tip calculator, this takes in the total cost of the meal a tip (through a button) and the total amount of people. Although when I am attempting to get the value of the button to do my calculation I am unable to do so.



I have tried to just run the function on load although that only gives me the value of the button once when the page loads and not when it has been clicked.



What I am trying to do is know when the button has been pressed, take the value from it and then use that for my further calculations although when attempting to do so the variable is undefined and I can not use it in my main calculations function.



How would I go about solving this?



Jsfiddle



HTML



<h1>Tip Calculator</h1>
<div class="inputs">
<input type="number" id="bill-amount" placeholder="Bill">
<div id="buttons">
<button value="5" class="btn">5%</button>
<button value="10" class="btn">10%</button>
<button value="15" class="btn">15%</button>
<button value="20" class="btn">20%</button>
<button value="30" class="btn">30%</button>
</div>
<input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">
</div>


JS



$("button").click(function() {
var buttonValue = $(this).val();
console.log(buttonValue);
});

function tipCalculator() {
var billAmount = $("#bill-amount").val();
var peopleAmount = $("#people-amount").val();
var total = (billAmount * buttonValue) / peopleAmount;
total = Math.round(total * 100) / 100;
total = total.toFixed(2);
}









share|improve this question























  • Just call your tipCalculator function from inside your click event

    – Caleb H.
    Nov 20 '18 at 23:43











  • @CalebH. I did this before and it does not work? I added tipCalculator(); after my console.log

    – Jacques Andre
    Nov 20 '18 at 23:44











  • you need to pass the value as a parameter to your tipCalculator function. Thats what Caleb meant

    – yBrodsky
    Nov 20 '18 at 23:47











  • You aren't logging the value of your "total" variable anywhere, so you won't see any output

    – Caleb H.
    Nov 20 '18 at 23:47






  • 1





    @JacquesAndre - danielarend's answer solved your problem and you've accepted it, but where he says "You should use a global var" I'd say "You can use a global var" ... because you really should *not* use a global var. The tipCalculator should be passed parameters and return the tip amount (or, arguably, the total w/tip) more in the way that jamesjaya's answer is laid out. If you're new to programming, you'll discover the horrors of globals soon enough. (but I wouldn't go so far as to say never use them)

    – Stephen P
    Nov 21 '18 at 0:08
















0















I am attempting to make a tip calculator, this takes in the total cost of the meal a tip (through a button) and the total amount of people. Although when I am attempting to get the value of the button to do my calculation I am unable to do so.



I have tried to just run the function on load although that only gives me the value of the button once when the page loads and not when it has been clicked.



What I am trying to do is know when the button has been pressed, take the value from it and then use that for my further calculations although when attempting to do so the variable is undefined and I can not use it in my main calculations function.



How would I go about solving this?



Jsfiddle



HTML



<h1>Tip Calculator</h1>
<div class="inputs">
<input type="number" id="bill-amount" placeholder="Bill">
<div id="buttons">
<button value="5" class="btn">5%</button>
<button value="10" class="btn">10%</button>
<button value="15" class="btn">15%</button>
<button value="20" class="btn">20%</button>
<button value="30" class="btn">30%</button>
</div>
<input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">
</div>


JS



$("button").click(function() {
var buttonValue = $(this).val();
console.log(buttonValue);
});

function tipCalculator() {
var billAmount = $("#bill-amount").val();
var peopleAmount = $("#people-amount").val();
var total = (billAmount * buttonValue) / peopleAmount;
total = Math.round(total * 100) / 100;
total = total.toFixed(2);
}









share|improve this question























  • Just call your tipCalculator function from inside your click event

    – Caleb H.
    Nov 20 '18 at 23:43











  • @CalebH. I did this before and it does not work? I added tipCalculator(); after my console.log

    – Jacques Andre
    Nov 20 '18 at 23:44











  • you need to pass the value as a parameter to your tipCalculator function. Thats what Caleb meant

    – yBrodsky
    Nov 20 '18 at 23:47











  • You aren't logging the value of your "total" variable anywhere, so you won't see any output

    – Caleb H.
    Nov 20 '18 at 23:47






  • 1





    @JacquesAndre - danielarend's answer solved your problem and you've accepted it, but where he says "You should use a global var" I'd say "You can use a global var" ... because you really should *not* use a global var. The tipCalculator should be passed parameters and return the tip amount (or, arguably, the total w/tip) more in the way that jamesjaya's answer is laid out. If you're new to programming, you'll discover the horrors of globals soon enough. (but I wouldn't go so far as to say never use them)

    – Stephen P
    Nov 21 '18 at 0:08














0












0








0








I am attempting to make a tip calculator, this takes in the total cost of the meal a tip (through a button) and the total amount of people. Although when I am attempting to get the value of the button to do my calculation I am unable to do so.



I have tried to just run the function on load although that only gives me the value of the button once when the page loads and not when it has been clicked.



What I am trying to do is know when the button has been pressed, take the value from it and then use that for my further calculations although when attempting to do so the variable is undefined and I can not use it in my main calculations function.



How would I go about solving this?



Jsfiddle



HTML



<h1>Tip Calculator</h1>
<div class="inputs">
<input type="number" id="bill-amount" placeholder="Bill">
<div id="buttons">
<button value="5" class="btn">5%</button>
<button value="10" class="btn">10%</button>
<button value="15" class="btn">15%</button>
<button value="20" class="btn">20%</button>
<button value="30" class="btn">30%</button>
</div>
<input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">
</div>


JS



$("button").click(function() {
var buttonValue = $(this).val();
console.log(buttonValue);
});

function tipCalculator() {
var billAmount = $("#bill-amount").val();
var peopleAmount = $("#people-amount").val();
var total = (billAmount * buttonValue) / peopleAmount;
total = Math.round(total * 100) / 100;
total = total.toFixed(2);
}









share|improve this question














I am attempting to make a tip calculator, this takes in the total cost of the meal a tip (through a button) and the total amount of people. Although when I am attempting to get the value of the button to do my calculation I am unable to do so.



I have tried to just run the function on load although that only gives me the value of the button once when the page loads and not when it has been clicked.



What I am trying to do is know when the button has been pressed, take the value from it and then use that for my further calculations although when attempting to do so the variable is undefined and I can not use it in my main calculations function.



How would I go about solving this?



Jsfiddle



HTML



<h1>Tip Calculator</h1>
<div class="inputs">
<input type="number" id="bill-amount" placeholder="Bill">
<div id="buttons">
<button value="5" class="btn">5%</button>
<button value="10" class="btn">10%</button>
<button value="15" class="btn">15%</button>
<button value="20" class="btn">20%</button>
<button value="30" class="btn">30%</button>
</div>
<input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">
</div>


JS



$("button").click(function() {
var buttonValue = $(this).val();
console.log(buttonValue);
});

function tipCalculator() {
var billAmount = $("#bill-amount").val();
var peopleAmount = $("#people-amount").val();
var total = (billAmount * buttonValue) / peopleAmount;
total = Math.round(total * 100) / 100;
total = total.toFixed(2);
}






javascript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 23:41









Jacques AndreJacques Andre

346




346













  • Just call your tipCalculator function from inside your click event

    – Caleb H.
    Nov 20 '18 at 23:43











  • @CalebH. I did this before and it does not work? I added tipCalculator(); after my console.log

    – Jacques Andre
    Nov 20 '18 at 23:44











  • you need to pass the value as a parameter to your tipCalculator function. Thats what Caleb meant

    – yBrodsky
    Nov 20 '18 at 23:47











  • You aren't logging the value of your "total" variable anywhere, so you won't see any output

    – Caleb H.
    Nov 20 '18 at 23:47






  • 1





    @JacquesAndre - danielarend's answer solved your problem and you've accepted it, but where he says "You should use a global var" I'd say "You can use a global var" ... because you really should *not* use a global var. The tipCalculator should be passed parameters and return the tip amount (or, arguably, the total w/tip) more in the way that jamesjaya's answer is laid out. If you're new to programming, you'll discover the horrors of globals soon enough. (but I wouldn't go so far as to say never use them)

    – Stephen P
    Nov 21 '18 at 0:08



















  • Just call your tipCalculator function from inside your click event

    – Caleb H.
    Nov 20 '18 at 23:43











  • @CalebH. I did this before and it does not work? I added tipCalculator(); after my console.log

    – Jacques Andre
    Nov 20 '18 at 23:44











  • you need to pass the value as a parameter to your tipCalculator function. Thats what Caleb meant

    – yBrodsky
    Nov 20 '18 at 23:47











  • You aren't logging the value of your "total" variable anywhere, so you won't see any output

    – Caleb H.
    Nov 20 '18 at 23:47






  • 1





    @JacquesAndre - danielarend's answer solved your problem and you've accepted it, but where he says "You should use a global var" I'd say "You can use a global var" ... because you really should *not* use a global var. The tipCalculator should be passed parameters and return the tip amount (or, arguably, the total w/tip) more in the way that jamesjaya's answer is laid out. If you're new to programming, you'll discover the horrors of globals soon enough. (but I wouldn't go so far as to say never use them)

    – Stephen P
    Nov 21 '18 at 0:08

















Just call your tipCalculator function from inside your click event

– Caleb H.
Nov 20 '18 at 23:43





Just call your tipCalculator function from inside your click event

– Caleb H.
Nov 20 '18 at 23:43













@CalebH. I did this before and it does not work? I added tipCalculator(); after my console.log

– Jacques Andre
Nov 20 '18 at 23:44





@CalebH. I did this before and it does not work? I added tipCalculator(); after my console.log

– Jacques Andre
Nov 20 '18 at 23:44













you need to pass the value as a parameter to your tipCalculator function. Thats what Caleb meant

– yBrodsky
Nov 20 '18 at 23:47





you need to pass the value as a parameter to your tipCalculator function. Thats what Caleb meant

– yBrodsky
Nov 20 '18 at 23:47













You aren't logging the value of your "total" variable anywhere, so you won't see any output

– Caleb H.
Nov 20 '18 at 23:47





You aren't logging the value of your "total" variable anywhere, so you won't see any output

– Caleb H.
Nov 20 '18 at 23:47




1




1





@JacquesAndre - danielarend's answer solved your problem and you've accepted it, but where he says "You should use a global var" I'd say "You can use a global var" ... because you really should *not* use a global var. The tipCalculator should be passed parameters and return the tip amount (or, arguably, the total w/tip) more in the way that jamesjaya's answer is laid out. If you're new to programming, you'll discover the horrors of globals soon enough. (but I wouldn't go so far as to say never use them)

– Stephen P
Nov 21 '18 at 0:08





@JacquesAndre - danielarend's answer solved your problem and you've accepted it, but where he says "You should use a global var" I'd say "You can use a global var" ... because you really should *not* use a global var. The tipCalculator should be passed parameters and return the tip amount (or, arguably, the total w/tip) more in the way that jamesjaya's answer is laid out. If you're new to programming, you'll discover the horrors of globals soon enough. (but I wouldn't go so far as to say never use them)

– Stephen P
Nov 21 '18 at 0:08












3 Answers
3






active

oldest

votes


















0














You should use a global var, and then you can read it all the time. Just declare it out of your function and is done:



var buttonValue;

$("button").click(function() {
buttonValue= $(this).val();
console.log(buttonValue);
});





share|improve this answer

































    0














    Call it in button click listener. Make tipCalculator receive one param and pass buttonValue value.



    function tipCalculator(buttonValue) {
    var billAmount = $("#bill-amount").val();
    var peopleAmount = $("#people-amount").val();
    var total = (billAmount * buttonValue) / peopleAmount;
    total = Math.round(total * 100);
    total = total.toFixed(2);
    alert(total);
    }

    $("button").click(function() {
    var buttonValue = $(this).val();
    console.log(buttonValue);
    tipCalculator(buttonValue);
    });





    share|improve this answer































      0














      In my opinion button is the wrong element for this. It does not maintain state and it does not show your users what they have selected. Use radio buttons instead. This is even more important should the data be send back to a server for processing at any-point as the selected tip value would be lost without persisting it into another form field.






      function tipCalculator() {
      var billAmount = $("#bill-amount").val();
      var peopleAmount = $("#people-amount").val();
      //Get the tip value
      var tip = $("[name=rdoTip]:checked").val();
      tip = (tip/100) + 1;
      console.log("Tip: " + tip);
      var total = (billAmount * tip) / peopleAmount;

      total = Math.round(total * 100) / 100 * 2;
      total = total.toFixed(2);
      console.log(total);
      }

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <h1>Tip Calculator</h1>
      <div class="inputs">
      <input type="number" id="bill-amount" placeholder="Bill">
      <fieldset>
      <legend>Tip</legend>
      <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
      <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
      <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
      <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
      <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
      <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
      </fieldset>
      </div>
      <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">





      You can make this a little prettier using CSS






      function tipCalculator() {
      var billAmount = $("#bill-amount").val();
      var peopleAmount = $("#people-amount").val();
      //Get the tip value
      var tip = $("[name=rdoTip]:checked").val();
      tip = (tip/100) + 1;
      console.log("Tip: " + tip);
      var total = (billAmount * tip) / peopleAmount;

      total = Math.round(total * 100) / 100 * 2;
      total = total.toFixed(2);
      console.log(total);
      }

      fieldset.tips {border:none; padding-left:0; margin-top:5px;}
      fieldset.tips legend {margin-left:0;}
      fieldset.tips input[type="radio"] {display:none}
      fieldset.tips label{padding:5px; border: solid red 1px; margin: 0 2px; border-radius: 5px; width: 40px; display:inline-block; text-align:center;}
      fieldset.tips input:checked + label {background-color:red; font-weight:bold; color: #FFF;

      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
      <h1>Tip Calculator</h1>
      <div class="inputs">
      <input type="number" id="bill-amount" placeholder="Bill">
      <fieldset class="tips">
      <legend>Tip</legend>
      <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
      <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
      <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
      <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
      <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
      <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
      </fieldset>
      </div>
      <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">








      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403256%2fhow-do-i-use-a-variable-multiple-times-in-different-functions%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        You should use a global var, and then you can read it all the time. Just declare it out of your function and is done:



        var buttonValue;

        $("button").click(function() {
        buttonValue= $(this).val();
        console.log(buttonValue);
        });





        share|improve this answer






























          0














          You should use a global var, and then you can read it all the time. Just declare it out of your function and is done:



          var buttonValue;

          $("button").click(function() {
          buttonValue= $(this).val();
          console.log(buttonValue);
          });





          share|improve this answer




























            0












            0








            0







            You should use a global var, and then you can read it all the time. Just declare it out of your function and is done:



            var buttonValue;

            $("button").click(function() {
            buttonValue= $(this).val();
            console.log(buttonValue);
            });





            share|improve this answer















            You should use a global var, and then you can read it all the time. Just declare it out of your function and is done:



            var buttonValue;

            $("button").click(function() {
            buttonValue= $(this).val();
            console.log(buttonValue);
            });






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 '18 at 23:52

























            answered Nov 20 '18 at 23:47









            danielarenddanielarend

            598413




            598413

























                0














                Call it in button click listener. Make tipCalculator receive one param and pass buttonValue value.



                function tipCalculator(buttonValue) {
                var billAmount = $("#bill-amount").val();
                var peopleAmount = $("#people-amount").val();
                var total = (billAmount * buttonValue) / peopleAmount;
                total = Math.round(total * 100);
                total = total.toFixed(2);
                alert(total);
                }

                $("button").click(function() {
                var buttonValue = $(this).val();
                console.log(buttonValue);
                tipCalculator(buttonValue);
                });





                share|improve this answer




























                  0














                  Call it in button click listener. Make tipCalculator receive one param and pass buttonValue value.



                  function tipCalculator(buttonValue) {
                  var billAmount = $("#bill-amount").val();
                  var peopleAmount = $("#people-amount").val();
                  var total = (billAmount * buttonValue) / peopleAmount;
                  total = Math.round(total * 100);
                  total = total.toFixed(2);
                  alert(total);
                  }

                  $("button").click(function() {
                  var buttonValue = $(this).val();
                  console.log(buttonValue);
                  tipCalculator(buttonValue);
                  });





                  share|improve this answer


























                    0












                    0








                    0







                    Call it in button click listener. Make tipCalculator receive one param and pass buttonValue value.



                    function tipCalculator(buttonValue) {
                    var billAmount = $("#bill-amount").val();
                    var peopleAmount = $("#people-amount").val();
                    var total = (billAmount * buttonValue) / peopleAmount;
                    total = Math.round(total * 100);
                    total = total.toFixed(2);
                    alert(total);
                    }

                    $("button").click(function() {
                    var buttonValue = $(this).val();
                    console.log(buttonValue);
                    tipCalculator(buttonValue);
                    });





                    share|improve this answer













                    Call it in button click listener. Make tipCalculator receive one param and pass buttonValue value.



                    function tipCalculator(buttonValue) {
                    var billAmount = $("#bill-amount").val();
                    var peopleAmount = $("#people-amount").val();
                    var total = (billAmount * buttonValue) / peopleAmount;
                    total = Math.round(total * 100);
                    total = total.toFixed(2);
                    alert(total);
                    }

                    $("button").click(function() {
                    var buttonValue = $(this).val();
                    console.log(buttonValue);
                    tipCalculator(buttonValue);
                    });






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 '18 at 23:54









                    jamesjayajamesjaya

                    657412




                    657412























                        0














                        In my opinion button is the wrong element for this. It does not maintain state and it does not show your users what they have selected. Use radio buttons instead. This is even more important should the data be send back to a server for processing at any-point as the selected tip value would be lost without persisting it into another form field.






                        function tipCalculator() {
                        var billAmount = $("#bill-amount").val();
                        var peopleAmount = $("#people-amount").val();
                        //Get the tip value
                        var tip = $("[name=rdoTip]:checked").val();
                        tip = (tip/100) + 1;
                        console.log("Tip: " + tip);
                        var total = (billAmount * tip) / peopleAmount;

                        total = Math.round(total * 100) / 100 * 2;
                        total = total.toFixed(2);
                        console.log(total);
                        }

                        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                        <h1>Tip Calculator</h1>
                        <div class="inputs">
                        <input type="number" id="bill-amount" placeholder="Bill">
                        <fieldset>
                        <legend>Tip</legend>
                        <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                        <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                        <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                        <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                        <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                        <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                        </fieldset>
                        </div>
                        <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">





                        You can make this a little prettier using CSS






                        function tipCalculator() {
                        var billAmount = $("#bill-amount").val();
                        var peopleAmount = $("#people-amount").val();
                        //Get the tip value
                        var tip = $("[name=rdoTip]:checked").val();
                        tip = (tip/100) + 1;
                        console.log("Tip: " + tip);
                        var total = (billAmount * tip) / peopleAmount;

                        total = Math.round(total * 100) / 100 * 2;
                        total = total.toFixed(2);
                        console.log(total);
                        }

                        fieldset.tips {border:none; padding-left:0; margin-top:5px;}
                        fieldset.tips legend {margin-left:0;}
                        fieldset.tips input[type="radio"] {display:none}
                        fieldset.tips label{padding:5px; border: solid red 1px; margin: 0 2px; border-radius: 5px; width: 40px; display:inline-block; text-align:center;}
                        fieldset.tips input:checked + label {background-color:red; font-weight:bold; color: #FFF;

                        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                        <h1>Tip Calculator</h1>
                        <div class="inputs">
                        <input type="number" id="bill-amount" placeholder="Bill">
                        <fieldset class="tips">
                        <legend>Tip</legend>
                        <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                        <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                        <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                        <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                        <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                        <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                        </fieldset>
                        </div>
                        <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">








                        share|improve this answer






























                          0














                          In my opinion button is the wrong element for this. It does not maintain state and it does not show your users what they have selected. Use radio buttons instead. This is even more important should the data be send back to a server for processing at any-point as the selected tip value would be lost without persisting it into another form field.






                          function tipCalculator() {
                          var billAmount = $("#bill-amount").val();
                          var peopleAmount = $("#people-amount").val();
                          //Get the tip value
                          var tip = $("[name=rdoTip]:checked").val();
                          tip = (tip/100) + 1;
                          console.log("Tip: " + tip);
                          var total = (billAmount * tip) / peopleAmount;

                          total = Math.round(total * 100) / 100 * 2;
                          total = total.toFixed(2);
                          console.log(total);
                          }

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                          <h1>Tip Calculator</h1>
                          <div class="inputs">
                          <input type="number" id="bill-amount" placeholder="Bill">
                          <fieldset>
                          <legend>Tip</legend>
                          <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                          <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                          <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                          <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                          <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                          <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                          </fieldset>
                          </div>
                          <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">





                          You can make this a little prettier using CSS






                          function tipCalculator() {
                          var billAmount = $("#bill-amount").val();
                          var peopleAmount = $("#people-amount").val();
                          //Get the tip value
                          var tip = $("[name=rdoTip]:checked").val();
                          tip = (tip/100) + 1;
                          console.log("Tip: " + tip);
                          var total = (billAmount * tip) / peopleAmount;

                          total = Math.round(total * 100) / 100 * 2;
                          total = total.toFixed(2);
                          console.log(total);
                          }

                          fieldset.tips {border:none; padding-left:0; margin-top:5px;}
                          fieldset.tips legend {margin-left:0;}
                          fieldset.tips input[type="radio"] {display:none}
                          fieldset.tips label{padding:5px; border: solid red 1px; margin: 0 2px; border-radius: 5px; width: 40px; display:inline-block; text-align:center;}
                          fieldset.tips input:checked + label {background-color:red; font-weight:bold; color: #FFF;

                          <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                          <h1>Tip Calculator</h1>
                          <div class="inputs">
                          <input type="number" id="bill-amount" placeholder="Bill">
                          <fieldset class="tips">
                          <legend>Tip</legend>
                          <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                          <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                          <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                          <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                          <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                          <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                          </fieldset>
                          </div>
                          <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">








                          share|improve this answer




























                            0












                            0








                            0







                            In my opinion button is the wrong element for this. It does not maintain state and it does not show your users what they have selected. Use radio buttons instead. This is even more important should the data be send back to a server for processing at any-point as the selected tip value would be lost without persisting it into another form field.






                            function tipCalculator() {
                            var billAmount = $("#bill-amount").val();
                            var peopleAmount = $("#people-amount").val();
                            //Get the tip value
                            var tip = $("[name=rdoTip]:checked").val();
                            tip = (tip/100) + 1;
                            console.log("Tip: " + tip);
                            var total = (billAmount * tip) / peopleAmount;

                            total = Math.round(total * 100) / 100 * 2;
                            total = total.toFixed(2);
                            console.log(total);
                            }

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <h1>Tip Calculator</h1>
                            <div class="inputs">
                            <input type="number" id="bill-amount" placeholder="Bill">
                            <fieldset>
                            <legend>Tip</legend>
                            <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                            <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                            <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                            <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                            <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                            <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                            </fieldset>
                            </div>
                            <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">





                            You can make this a little prettier using CSS






                            function tipCalculator() {
                            var billAmount = $("#bill-amount").val();
                            var peopleAmount = $("#people-amount").val();
                            //Get the tip value
                            var tip = $("[name=rdoTip]:checked").val();
                            tip = (tip/100) + 1;
                            console.log("Tip: " + tip);
                            var total = (billAmount * tip) / peopleAmount;

                            total = Math.round(total * 100) / 100 * 2;
                            total = total.toFixed(2);
                            console.log(total);
                            }

                            fieldset.tips {border:none; padding-left:0; margin-top:5px;}
                            fieldset.tips legend {margin-left:0;}
                            fieldset.tips input[type="radio"] {display:none}
                            fieldset.tips label{padding:5px; border: solid red 1px; margin: 0 2px; border-radius: 5px; width: 40px; display:inline-block; text-align:center;}
                            fieldset.tips input:checked + label {background-color:red; font-weight:bold; color: #FFF;

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <h1>Tip Calculator</h1>
                            <div class="inputs">
                            <input type="number" id="bill-amount" placeholder="Bill">
                            <fieldset class="tips">
                            <legend>Tip</legend>
                            <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                            <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                            <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                            <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                            <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                            <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                            </fieldset>
                            </div>
                            <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">








                            share|improve this answer















                            In my opinion button is the wrong element for this. It does not maintain state and it does not show your users what they have selected. Use radio buttons instead. This is even more important should the data be send back to a server for processing at any-point as the selected tip value would be lost without persisting it into another form field.






                            function tipCalculator() {
                            var billAmount = $("#bill-amount").val();
                            var peopleAmount = $("#people-amount").val();
                            //Get the tip value
                            var tip = $("[name=rdoTip]:checked").val();
                            tip = (tip/100) + 1;
                            console.log("Tip: " + tip);
                            var total = (billAmount * tip) / peopleAmount;

                            total = Math.round(total * 100) / 100 * 2;
                            total = total.toFixed(2);
                            console.log(total);
                            }

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <h1>Tip Calculator</h1>
                            <div class="inputs">
                            <input type="number" id="bill-amount" placeholder="Bill">
                            <fieldset>
                            <legend>Tip</legend>
                            <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                            <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                            <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                            <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                            <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                            <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                            </fieldset>
                            </div>
                            <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">





                            You can make this a little prettier using CSS






                            function tipCalculator() {
                            var billAmount = $("#bill-amount").val();
                            var peopleAmount = $("#people-amount").val();
                            //Get the tip value
                            var tip = $("[name=rdoTip]:checked").val();
                            tip = (tip/100) + 1;
                            console.log("Tip: " + tip);
                            var total = (billAmount * tip) / peopleAmount;

                            total = Math.round(total * 100) / 100 * 2;
                            total = total.toFixed(2);
                            console.log(total);
                            }

                            fieldset.tips {border:none; padding-left:0; margin-top:5px;}
                            fieldset.tips legend {margin-left:0;}
                            fieldset.tips input[type="radio"] {display:none}
                            fieldset.tips label{padding:5px; border: solid red 1px; margin: 0 2px; border-radius: 5px; width: 40px; display:inline-block; text-align:center;}
                            fieldset.tips input:checked + label {background-color:red; font-weight:bold; color: #FFF;

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <h1>Tip Calculator</h1>
                            <div class="inputs">
                            <input type="number" id="bill-amount" placeholder="Bill">
                            <fieldset class="tips">
                            <legend>Tip</legend>
                            <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                            <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                            <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                            <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                            <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                            <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                            </fieldset>
                            </div>
                            <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">








                            function tipCalculator() {
                            var billAmount = $("#bill-amount").val();
                            var peopleAmount = $("#people-amount").val();
                            //Get the tip value
                            var tip = $("[name=rdoTip]:checked").val();
                            tip = (tip/100) + 1;
                            console.log("Tip: " + tip);
                            var total = (billAmount * tip) / peopleAmount;

                            total = Math.round(total * 100) / 100 * 2;
                            total = total.toFixed(2);
                            console.log(total);
                            }

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <h1>Tip Calculator</h1>
                            <div class="inputs">
                            <input type="number" id="bill-amount" placeholder="Bill">
                            <fieldset>
                            <legend>Tip</legend>
                            <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                            <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                            <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                            <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                            <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                            <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                            </fieldset>
                            </div>
                            <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">





                            function tipCalculator() {
                            var billAmount = $("#bill-amount").val();
                            var peopleAmount = $("#people-amount").val();
                            //Get the tip value
                            var tip = $("[name=rdoTip]:checked").val();
                            tip = (tip/100) + 1;
                            console.log("Tip: " + tip);
                            var total = (billAmount * tip) / peopleAmount;

                            total = Math.round(total * 100) / 100 * 2;
                            total = total.toFixed(2);
                            console.log(total);
                            }

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <h1>Tip Calculator</h1>
                            <div class="inputs">
                            <input type="number" id="bill-amount" placeholder="Bill">
                            <fieldset>
                            <legend>Tip</legend>
                            <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                            <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                            <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                            <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                            <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                            <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                            </fieldset>
                            </div>
                            <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">





                            function tipCalculator() {
                            var billAmount = $("#bill-amount").val();
                            var peopleAmount = $("#people-amount").val();
                            //Get the tip value
                            var tip = $("[name=rdoTip]:checked").val();
                            tip = (tip/100) + 1;
                            console.log("Tip: " + tip);
                            var total = (billAmount * tip) / peopleAmount;

                            total = Math.round(total * 100) / 100 * 2;
                            total = total.toFixed(2);
                            console.log(total);
                            }

                            fieldset.tips {border:none; padding-left:0; margin-top:5px;}
                            fieldset.tips legend {margin-left:0;}
                            fieldset.tips input[type="radio"] {display:none}
                            fieldset.tips label{padding:5px; border: solid red 1px; margin: 0 2px; border-radius: 5px; width: 40px; display:inline-block; text-align:center;}
                            fieldset.tips input:checked + label {background-color:red; font-weight:bold; color: #FFF;

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <h1>Tip Calculator</h1>
                            <div class="inputs">
                            <input type="number" id="bill-amount" placeholder="Bill">
                            <fieldset class="tips">
                            <legend>Tip</legend>
                            <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                            <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                            <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                            <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                            <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                            <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                            </fieldset>
                            </div>
                            <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">





                            function tipCalculator() {
                            var billAmount = $("#bill-amount").val();
                            var peopleAmount = $("#people-amount").val();
                            //Get the tip value
                            var tip = $("[name=rdoTip]:checked").val();
                            tip = (tip/100) + 1;
                            console.log("Tip: " + tip);
                            var total = (billAmount * tip) / peopleAmount;

                            total = Math.round(total * 100) / 100 * 2;
                            total = total.toFixed(2);
                            console.log(total);
                            }

                            fieldset.tips {border:none; padding-left:0; margin-top:5px;}
                            fieldset.tips legend {margin-left:0;}
                            fieldset.tips input[type="radio"] {display:none}
                            fieldset.tips label{padding:5px; border: solid red 1px; margin: 0 2px; border-radius: 5px; width: 40px; display:inline-block; text-align:center;}
                            fieldset.tips input:checked + label {background-color:red; font-weight:bold; color: #FFF;

                            <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
                            <h1>Tip Calculator</h1>
                            <div class="inputs">
                            <input type="number" id="bill-amount" placeholder="Bill">
                            <fieldset class="tips">
                            <legend>Tip</legend>
                            <input type="radio" name="rdoTip" value="0" id="rdoTip0" checked /><label for="rdoTip0">0%</label>
                            <input type="radio" name="rdoTip" value="5" id="rdoTip5" /><label for="rdoTip5">5%</label>
                            <input type="radio" name="rdoTip" value="10" id="rdoTip10" /><label for="rdoTip10">10%</label>
                            <input type="radio" name="rdoTip" value="15" id="rdoTip15" /><label for="rdoTip15">15%</label>
                            <input type="radio" name="rdoTip" value="20" id="rdoTip20" /><label for="rdoTip20">20%</label>
                            <input type="radio" name="rdoTip" value="30" id="rdoTip30" /><label for="rdoTip30">30%</label>
                            </fieldset>
                            </div>
                            <input type="number" id="people-amount" placeholder="# Of people" onkeyup="tipCalculator()">






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 21 '18 at 0:22

























                            answered Nov 21 '18 at 0:11









                            Jon PJon P

                            12k73459




                            12k73459






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53403256%2fhow-do-i-use-a-variable-multiple-times-in-different-functions%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                這個網誌中的熱門文章

                                Tangent Lines Diagram Along Smooth Curve

                                Yusuf al-Mu'taman ibn Hud

                                Zucchini