0

I have been having some issues accessing and updating variables declared inside document.ready function. Everytime i attempt to do alerts inside my functions the values of these functions. Here is my document. ready function with variables and functions within them. Any help at all with this would be greatly appreciated.

 /***********************************************************/ 
        var bdv; 
        var mv; 
        var cYear; 
        var _miles; 
        var cpo_input;

 /* -------------------------------------------------*/

        var _prestige;
        var car_type; 
        var acc_history;
        var _aam;
        var avg_mileage;

 /***********************************************************/  
$(document).ready(function() {

/***********************************************************/ 
        bdv = $("#base_dv").val(); 
        mv = $('input:text[name="field8"]').val(); 
        cYear = $('input:text[name="field2"]').val(); 
        _miles = $('input:text[name="field5"]').val(); 
        cpo_input = $('input:text[name="field19"]');

 /* -------------------------------------------------*/

        _prestige = 0.00;
        car_type= 0; 
        acc_history = 0;
        _aam = 0.00;
        avg_mileage = 0.00;

 /***********************************************************/             
     if ('<?php echo $job[0]['
     job_type '] ?>' == "33") {

         $('input:radio[name="field32"][value = val1]').attr("disabled", true);
         $('input:radio[name="field32"][value = val2]').attr("disabled", true);

         $("[name='field20[]']").each(function () {
             $(this).change(function () {
                 $('input:radio[name="field7"]').each(function () {
                     if (!$('input:radio[name="field7"]').is(':checked')) {
                         alert("Please Select 2 or 4 door car");
                         $("[name='field20[]']").each(function () {
                             $(this).removeAttr('checked');
                         });
                     } else if ($('input:radio[name="field7"]').is(':checked')) {
                         return false;
                     }
                 });
                 $(this).click(function () {
                     bdv = damage_severity(mv, bdv);
                     _aam = average_annual_mileage_factor(_miles, mv);
                     avg_mileage = avg_mileage_calc(_miles, cYear);

                     alert("Market Value: " + mv);
                     alert("Miles Reporting: " + _miles);
                     alert("Base DV:" + bdv);
                     alert("Average Annual Mileage" + _aam);
                     alert("Average Mileage outside func: " + avg_mileage);
                 });
             });
         });
     });
6
  • 1
    variables will only be accessible in same or lower scope. But here, i don't see any issue Commented Jan 17, 2014 at 21:13
  • A.Wolff Im not sure why it does why I get the 0's either, even after declaring them outside the .ready scope and initializing them inside the .ready scope. Commented Jan 17, 2014 at 21:28
  • Teemu sorry about that, I probably was not fully focusing. I meant the values I am alerting arent correct. They are the original declared values. About the return false, I was not sure of a better way to exit the loop once the condition was met. Commented Jan 17, 2014 at 21:30
  • Teemu - I'm a bit green to most of this, what would your advice be in regards to making this more efficient? Commented Jan 17, 2014 at 21:45
  • Umh... please forget the return issue, in jQuery it's OK. But attaching a new click handler every time something is changed is not probably what you want. How are the arguments named in damage_severity() and other function's declarations? if you've the same names (_miles, mv), the original outer values are shadowed in that function. Commented Jan 17, 2014 at 21:47

2 Answers 2

2

Declare them outside of .ready(). Populate them inside .ready().

Also, there's a lot of functionality that you have, that does not need to be in .ready() in the first place.

Sign up to request clarification or add additional context in comments.

2 Comments

What is the issue in OP's code, i cannot spot it. OK i see, OP is posting code which works, not the one which doesn't work
I did that and now it does not alert out the values, Im not sure why
0

bdv, _aam and avg_mileage seems to have the same values as they had when document.ready was triggered, since you never update these values before using them.

I'd move the declarations back to the $(document).ready() and do something like this:

$(document).ready(function () {
    var  bdv, aam =  ...,
         $bdv = $("#base_dv"),
         $mv = $('input:text[name="field8"]'),
                 :
         ;
                 :
    $(this).click(function () {
        bdv = damage_severity($mv.val(), $bdv.val());
                 :
    });
                 :
});

Notice also, that you've to move the $(this).click(); out from $(this).change();. As it is now, your code will attach a new click listener to the element every time onchange is triggered on that element.

2 Comments

Using bdv = damage_severity($mv.val(), $bdv.val()); actually returned an error saying that $mv.val() isnt a function
Are you sure you've assigned an actual jQuery object to them, instead of .val()? Please take a new look on the variable declaration in my code.

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.