0

I'm not sure why the each iteration is not alerting Table Two value.

$(document).ready(function () {
    Test1([{id:"one",tbl:"Table One"},{id:"two",tbl:"Table Two"}]);
});

function Test1(arrObj) {
                   
    $.each(arrObj,function(idx){
        var thing = $(this);
        alert(thing[idx].tbl); //Not getting to Table Two
    
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1
  • Uncaught TypeError: Cannot read property 'tbl' of undefined Commented Mar 10, 2015 at 21:25

2 Answers 2

3

Looking at the documentation for $.each() you can see that a second argument is passed to the callback representing the value of the element. You can use it like this:

$(document).ready(function () {
    Test1([{id:"one",tbl:"Table One"},{id:"two",tbl:"Table Two"}]);
});

function Test1(arrObj) {
                   
    $.each(arrObj, function(idx, thing){
        alert(thing.tbl);
    
    });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

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

Comments

1

You mustn't wrap this into $(). Because this is already a value of each element of the array arrObj. You can do so, and it will be enough, jsFiddle:

function Test1(arrObj) {

    $.each(arrObj,function(){
        alert(this.tbl);
    });
}

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.