Grafit
Grafit

Reputation: 699

Add item on specific position of JQuery array

I got the following array:

var liList = $(".paging li");

Right now it's filled like this: ["Previous", "Next"].
Now I want to add numbers between those two items to get something like ["Previous", "1", "2", "Next"].
How do I do this? I want something like this:

var liList = $(".paging li");
for (var i = 1; i < 10; i++){
    liList.eq(i).add("li");
} 

Upvotes: 2

Views: 72

Answers (1)

vaso123
vaso123

Reputation: 12391

You can use the array_splice function:

$(function() {
    var pagination = ["Previous", "Next"];
    for (i = 1; i <= 10; i++ ) {
        pagination.splice(i, 0, i);
    }
    console.log(pagination);
});

Output is:

 ["Previous", 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, "Next"]

Upvotes: 1

Related Questions