Friday, 27 August 2021

Can the function be execute without any events like onclick or onload

Would appreciate a little help if you don't mind

Have to ask in same question because : due to down vote on this question I can only ask 1 question a week but the problem is related to same question so I asked it here . And it is the 1st question I asked so far

  • Have to execute function without event so that discount can be displayed along with prices at the start without clicking or any other event like :

what I need to do :
₹5,978 (Actual Price)
₹3,999 (Discounted Price)
33% off (Discount)

but this is happening :
₹5,978 (Actual Price)
₹3,999 (Discounted Price)
onclick event - User click on above prices and Discount is shown
33% off (Discount-Shown when user click above prices)

Can it be done without any event and function is executed automatic without an event

Let me know if you need clarification.

You can see in below snippet that 1st one is running automatic while 2nd one is running on click . Can it possible to run 2nd one as automatic because it solves my most of issues

function discount1() {
  var saadTotal = document.getElementsByClassName("Total1")[0].innerHTML;
  var saad1 = saadTotal.slice(1);
  var saad2 = saad1.split(",");
  var saad3 = saad2[0] + saad2[1];
  var saad4 = Number(saad3)
  var saad = document.getElementsByClassName("DiscPrice1")[0].innerHTML;
  var saad5 = saad.slice(1);
  var saad6 = saad5.split(",");
  var saad7 = saad6[0] + saad6[1];
  var saad8 = Number(saad7)
  var reed = ((saad4 - saad8) / saad4) * 100
  document.getElementsByClassName("demo1")[0].innerHTML = reed.toFixed(0) + "% off";
}
discount1();

function discount(reed) {
  var saadTotal = reed.parentElement.getElementsByClassName("Total")[0].innerHTML;
  var saad1 = saadTotal.slice(1);
  var saad2 = saad1.split(",");
  var saad3 = saad2[0] + saad2[1];
  var saad4 = Number(saad3)
  var saad = reed.parentElement.getElementsByClassName("DiscPrice")[0].innerHTML;
  var saad5 = saad.slice(1);
  var saad6 = saad5.split(",");
  var saad7 = saad6[0] + saad6[1];
  var saad8 = Number(saad7)
  var feed = ((saad4 - saad8) / saad4) * 100
  reed.getElementsByClassName("demo")[0].innerHTML = feed.toFixed(0) + "% off";
}
discount();
<div>
  <div class="daad" onclick="discount1()">
    <div class="Total1">&#x20B9;7,978</div>
    <div class="DiscPrice1">&#x20B9;3,999</div>
    <div class="demo1"></div>
  </div>
</div>

<br>

<div>
  <div class="daad" onclick="discount(this)">
    <div class="Total">&#x20B9;5,978</div>
    <div class="DiscPrice">&#x20B9;3,999</div>
    <div class="demo"></div>
  </div>
</div>


from Can the function be execute without any events like onclick or onload

No comments:

Post a Comment