1

i'v got this functions at my script:

(document).ready(function () {
$(".plcardFirst").change(function() {
valueFirst = $( ".plcardFirst" ).val();
        });
$(".plcardSecond").change(function() {
valueSecond = $( ".plcardSecond" ).val();
    });
});

How can i use valueFirst and valueSecond values to calculate and run an if statement? many thanks

2 Answers 2

2

Note that javascript have function scope and not block scope which means variables declared inside a function with the keyword var can be used inside that function only. But here you are declaring valueFirst and valueSecond without the keyword var, so they are both considered as a global variables so you can access them anytime you want and anywhere even inside an if statement

You can also use this way:

$(document).ready(function () {
var valueFirst,valueSecond; // declaring both variables at the beginning after document.ready

$(".plcardFirst").change(function() {
valueFirst = $( ".plcardFirst" ).val();
        });
$(".plcardSecond").change(function() {
valueSecond = $( ".plcardSecond" ).val();
    });

// now you can use valueFirst and valueSecond of course if $( ".plcardFirst" ).val() and $( ".plcardSecond" ).val(); return something

//example if(valueFirst == valueSecond){alert('both values are equal ');}
});
Sign up to request clarification or add additional context in comments.

Comments

0

Use as

 var valueFirst =0,valueSecond =0;
 (document).ready(function () {
       $(".plcardFirst").change(function() {
            valueFirst = $( ".plcardFirst" ).val();
       });
       $(".plcardSecond").change(function() {
           valueSecond = $( ".plcardSecond" ).val();
       });
 });

Comments

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.