0

Hi guys i am confused why have different result in my code. In "var rq" i just have one true alert ( just ir have true value). In first alert i have "ir" and its true but with second alert its shown "rj" and its not true !! what happen here can someone tell me? how i can fix that.

$('#friends').removeAttr('checked').on('click', function(){ 

var rq = {
    "mr" : $('li#mr a').attr("aria-expanded"), // false
    "ir" : $('li#ir a').attr("aria-expanded"), // true
    "rj" : $('li#rj a').attr("aria-expanded")  // false
};

if(this.checked) {
    for (var i in rq) {
        if ( rq[i] == "true"){

            alert(i);   // alert ir

            $('#checkAll-'+i).change(function () {

                alert(i);   // alert rj

                $( 'input:checkbox[id='+i+'-checkbox]' ).prop( "checked", true );
            });
        }
    }
}
)}

EDITE

HTML:

<input name="form-field-radio" type="radio" id="checkAll-ir" class="ace">
CHARLI
  • 27
  • 6
  • The `alert()` in the "change" handler won't happen until the targetted element actually changes. At that point `i` will be `rj` because that's the last value it had when that `for ... in` loop finished. – Pointy Apr 09 '16 at 14:11
  • But my element changed with success else the second alert must don't alerted – CHARLI Apr 09 '16 at 14:24
  • The value of `i` will be the value it had when the `for ... in` loop finished, not the value it had when the "change" handler was established. – Pointy Apr 09 '16 at 14:30
  • pls can you help more – CHARLI Apr 09 '16 at 14:43
  • You've got a variable, `i`. That will change because of the way the `for ... in` loop works, right? That variable is referenced in the "change" handler. When the change happens and the event handler runs, the value of "i" in the alert will be what it was when the `for` loop finished. – Pointy Apr 09 '16 at 14:44

0 Answers0