Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

150
Views
how can i make this script show the value of the var "per" based on the new value of the var "a" that raise on every click

I want to be able to display a 'clicker' variable, storing the number of clicks on a certain element, and then display it when i click on another element.

per = a * 6.6666 so I tried again and it wont work the result stay 0 but if I change the value of a to 1 it shows 6.6666 so the calculation is working fine but it is not taking the value of a based on how many times I click it just take the value I gave in the script ...

    var a =  0 ;
    const six = 6.6666;
            
    var per = a * six;

    $(".mychoice").click(function () {
        (a++);
    });

    $(".show").click(function () {
        $("#ss").text( per );
    });
about 4 years ago · Santiago Trujillo
3 answers
Answer question

0

Calculations are done when you tell them to be done.

If you change the value of a in a click event, then that value doesn't travel back through time so that a is the new value back when you read it and used it to multiply six.

If you want to redo that calculation when something is clicked then you need to write that expression in that click event handler function.

about 4 years ago · Santiago Trujillo Report

0

Keep in mind that a and per are two distinct locations in memory, therefore, you have to update the per variable along with the a variable.

var a = 0;
const six = 6.6666;
var per = a * six;
$(".mychoice").click(function () {
    (a++);
    // memory location labelled "a" has changed
    // update memory location labelled "per" accordingly
    per = a * six;
});

$(".show").click(function () {
    $("#ss").text(per);
});
about 4 years ago · Santiago Trujillo Report

0

var per = a * six;

The calculation is executed and stored in the per variable as soon as the line is executed, not when you reference the per variable.

If you want to calculate the value at a later time, use an (arrow) function instead.

// define `per` as arrow function with 0 arguments
var per = () => a * six;

Then you evoke the function when you need the result:

$(".show").click(function () {
    $("#ss").text( per() );
});

In this scenario doing the calculation directly, without defining a function, might be cleaner.

$(".show").click(function () {
    $("#ss").text(a * six);
});
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!