0

A Explanation for what i am doing.

I have ul that have li that has visiting card in it and the amout vary which . i want to add extra li to show the description of the visting card that i clicked. for ex if i clicked 1st 2nd or 3rd card i want to add new li in 4th place if i clicked 4th 5th or 6th then i want to add new li in 7th place.

For this i made an function to split the li in to multidimensional array having three li inside an array. and that mulidimensinal array is call chunks. now i got the write. but i am not able to appened the new li in my required place.

And i am getting this error TypeError: chunks[i] is undefined

And i given the code below

chunks[i] is undefined

chunks = [ 
    [li.pL14, li, li.pR15], 
    [li.pL14, li, li.pR15], 
    [li.pL14, li, li.pR15], 
    [li.pL14, li, li.pR15], 
    [li.pL14, li, li.pR15]
]

for (var i=0; i<chunks.length; i++) {    
    for (var j=0; j<chunks[i].length; j++) {
        $(chunks[i][j]).click(function() {
             $(chunks[i][j]).append('<li class="description"> <div class="cardDesCont mT1Mi"></div></li>');
        });
    }
}

Why it that happening?

3
  • Why would you add a click event to every element? Use event delegation on the parent to handle it. Commented Apr 1, 2013 at 18:40
  • 1
    This would be the second time you asked this, what did'nt work from your first question ? Commented Apr 1, 2013 at 18:41
  • What is li defined as? Commented Apr 1, 2013 at 18:44

1 Answer 1

1

You have i++ where you should have j++, in the inner loop. So you are incrementing i beyond the array bounds. Your code should be:

for (var i=0; i<chunks.length; i++) {   
    for (var j=0; j<chunks[i].length; j++) {
        // --------------------- HERE ^^^
        $(chunks[i][j]).click(function() {
            console.log('script works');
        });
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.