0

Hello I have a prboblem with the next code:

function loadOptions(num){
listTabs = new Array();
for(var i = 1 ; i < parseInt(num) + 1 ; i++){
    var tabActu = {
        'name':'tab'+i,
        'src':'urlImatge'
        };
    listTabs.add(tabActu);
    $.each(listTabs,function(key,value){
        alert(key+" : "+value);
    });
}

}

I need to create an list of elements equal to the num parameter. I can't find the error.

1
  • 1
    Please go back and accept some of the answers to your previous questions. It will help you get answers in the future. +1 to @RobW Commented Sep 24, 2011 at 21:46

2 Answers 2

2

Did you look in the error console for javascript errors?

Javascript arrays don't have a .add() method. You can use .push().

function loadOptions(num){
    listTabs = new Array();
    var len = parseInt(num, 10);
    for (var i = 1 ; i < len + 1 ; i++) {
        var tabActu = {
            'name':'tab' + i,
            'src':'urlImatge'
            };
        listTabs.push(tabActu);
        $.each(listTabs,function(key,value){
            alert(key+" : "+value);
        });
    }
}

In addition to change to .push(), parseInt must always be passed the radix value and you should remove the function call to parseInt from the loop so it's not called on every iteration. Also, you haven't delcared listTabs here so that makes it a global variable. Is that what you intended?

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

Comments

1

Sup Francesc

Arrays dont have a add method ..... use push

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.