# Adding an item to a repeater with code

**URL:** <https://forum.wixstudio.com/t/adding-an-item-to-a-repeater-with-code/1835>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [April 12, 2023, 12:07am UTC](https://forum.wixstudio.com/t/adding-an-item-to-a-repeater-with-code/1835 "2023-04-12T00:07:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gustaf-lindstrom](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gustaf-lindstrom/32/36986_2.png) [@gustaf-lindstrom](https://forum.wixstudio.com/u/gustaf-lindstrom)\
**Post date:** [April 12, 2023, 12:07am UTC](https://forum.wixstudio.com/t/adding-an-item-to-a-repeater-with-code/1835/1 "2023-04-12T00:07:15Z")

</div>

Hi guys!

I want to lengthen my repeater by one item with the click of a button, and I can’t figure it out. This is where I’m at so far:

$w . onReady ( **function** () {

```
**var** itemNumber = 1 ; 
**const** itemData = [ 
        { 
    _id : itemNumber ,  
    name : '' ,  
    title : '' ,  
    image : '' 
} 

```

];

```
$w ( "#anecdoteRepeater" ). data = itemData ; 

```

$w ( “#addAnecdote” ). onClick (() =\> {  
console . log ( “Clicked” );  
// Increment the itemNumber variable  
itemNumber = itemNumber + 1 ;

```
// Create a new item data object with default values 
**const** newItemData = [{ 
    _id : itemNumber , 
    name : '' , 
    title : '' , 
    image : '' 
}]; 

// Push the new item data to the itemData array 
itemData . push ( newItemData ); 
console . log ( itemData ); 

// Set the updated itemData array as the repeater's data 
$w ( "#anecdoteRepeater" ). data = itemData ; 

```

});  
});

---

<div class="post-metadata">

**Author:** ![mrmacaronager](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mrmacaronager/32/8640_2.png) [@mrmacaronager](https://forum.wixstudio.com/u/mrmacaronager)\
**Post date:** [April 12, 2023, 1:44am UTC](https://forum.wixstudio.com/t/adding-an-item-to-a-repeater-with-code/1835/2 "2023-04-12T01:44:57Z")

</div>

In the newItemData assignment, instead of an array with an object inside, try just a plain object.

```javascript
$w.onReady(function () {

    var itemNumber = 1;
    const itemData = [
            {
        _id: itemNumber, 
        name: '', 
        title: '', 
        image: ''
    }
];

    $w("#anecdoteRepeater").data = itemData;

$w("#addAnecdote").onClick(() => {
    console.log("Clicked");
    // Increment the itemNumber variable
    itemNumber = itemNumber + 1;

    // Create a new item data object with default values
    const newItemData = {
        _id: itemNumber,
        name: '',
        title: '',
        image: ''
    };

    // Push the new item data to the itemData array
    itemData.push(newItemData);
    console.log(itemData);

    // Set the updated itemData array as the repeater's data
    $w("#anecdoteRepeater").data = itemData;
});
});

```

---

<div class="post-metadata">

**Author:** ![gustaf-lindstrom](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gustaf-lindstrom/32/36986_2.png) [@gustaf-lindstrom](https://forum.wixstudio.com/u/gustaf-lindstrom)\
**Post date:** [April 12, 2023, 8:10am UTC](https://forum.wixstudio.com/t/adding-an-item-to-a-repeater-with-code/1835/3 "2023-04-12T08:10:57Z")

</div>

Same result at before. In the log, itemData keeps getting longer and longer, so you’d think it’s working, but the repeater just stays the same length.

---

<div class="post-metadata">

**Author:** ![gustaf-lindstrom](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/gustaf-lindstrom/32/36986_2.png) [@gustaf-lindstrom](https://forum.wixstudio.com/u/gustaf-lindstrom)\
**Post date:** [April 12, 2023, 8:30am UTC](https://forum.wixstudio.com/t/adding-an-item-to-a-repeater-with-code/1835/4 "2023-04-12T08:30:30Z")

</div>

I figured it out! The issue was that \_id needed to be a string. I tried converting them to strings by going ‘’ + itemNumber, and suddenly it worked!

```javascript
//------------ NEW ITEM TO REPEATER

$w.onReady(function () {

    var itemNumber = 1;
    const itemData = [{
            _id: '' + itemNumber,
            name: 'Amy Allen',
            title: 'CEO',
            image: 'https://static.wixstatic.com/media/4e87e18894e24c238e5724b887362eeb.jpg'
        },
    ];

    $w("#anecdoteRepeater").data = itemData;

    $w("#addAnecdote").onClick(() => {
        console.log("Clicked");
        // Increment the itemNumber variable
        itemNumber = itemNumber + 1;

        // Create a new item data object with default values
        const newItemData = {
            _id: '' + itemNumber,
            name: '',
            title: '',
            image: ''
        };

        // Push the new item data to the itemData array
        itemData.push(newItemData);
        console.log(itemData);

        // Set the updated itemData array as the repeater's data
        $w("#anecdoteRepeater").data = itemData;
    });
});

```
