Javascript simple function doesn't work

function pairPrices(sortPrices) {
  var totalAfterSale = 0;
  var totalSale = 0;
  
   for (i=0; i < sortPrices.length; i++) {
     totalAfterSale += sortPrices[i];
   }
   $(".total-sale").html("total after sale " + totalAfterSale);
  
}

Open in new window


Why it doesn't insert totalAfterSale to already created paragraph with .total-sale class ?

* codepen here http://goo.gl/PVU5fb
LVL 1
Fajer39Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mukesh YadavFull Stack DeveloperCommented:
Because when you are trying to update html of "paragraph with .total-sale class" it doesn't exists.

It's removed by the code piece:
$("#sale").on("click", function(e) {
    e.preventDefault;
    $(".after-sale > p").remove();
    calculateSale(prices, sortPrices);
  });

Open in new window


Line removing all p elements from after-sale class element is:

$(".after-sale > p").remove();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
Where in your html is there  a .total-sale class?

The reason your function does not work is that you have not created an element with a .total-sale class in it.
0
Fajer39Author Commented:
Thank You, I completely forgot that I'm deleting all paragraphs in the beginning.
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

Julian HansenCommented:
Maybe you meant to do this

 $("#sale").on("click", function(e) {
    e.preventDefault;
    $(".after-sale > p").html(''); // CLEAR THE CONTENTS OF .total-sale HERE
    calculateSale(prices, sortPrices);
  });

Open in new window

0
Alexandre SimõesManager / Technology SpecialistCommented:
Damn man, this took me a while! :p
0
Fajer39Author Commented:
Haha, maybe next time :-). Have a nice weekend
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.