# onClick function to save repeater item as favourite

**URL:** <https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990>\
**Category:** Ask the community\
**Tags:** code, question\
**Created:** [December 8, 2020, 2:00am UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990 "2020-12-08T02:00:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![events32996](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/events32996/32/16198_2.png) [@events32996](https://forum.wixstudio.com/u/events32996)\
**Post date:** [December 8, 2020, 2:00am UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990/1 "2020-12-08T02:00:48Z")

</div>

Hi All!

I have users posts being shown on #repeater1 on a dynamic page. I have two collections, ’ _marks’_ where the users uploaded content for their post is saved.  
The other collection being _‘favourites’,_ where the users favourites posts are saved. This collection has a _userID_ field and a _mark_ field.

1. There is an empty heart icon (#saveSong) within the repeater container for the user to click to save the item as a favourite. There is also a filled heart (#songSaved) shown to show that the item has been saved. If the user has saved an item but refresh’s the page, or returns back to this page, or see’s this post again, the #saveSong icon is shown rather than #songSaved? If the user has already saved an item, the filled heart is shown and the empty heart is hidden.

2. When a user clicked on the favourite button (#saveSong) in the repeater, it creates a new line in the #favourites collection. How do I set it so that only one line is created per user and the _marks_ field contains all of their favourited items?

3. Lastly, is there any coding to remove/unfavourite an item from the _marks_ field in the #favourites collection?

Here is my relevant current code:

```javascript
 $w("#repeater1").onItemReady(($item, itemData, index) => {

 wixData.query("favourites")
                    .find()
                    .then((results) => {
 if (results.items._id === itemData._id) {
                            $item("#saveSong").hide();
                            $item("#songSaved").show();
                        } else {
                            $item("#saveSong").show();
                            $item("#songSaved").hide();
                        }
///other code
}
 $item("#saveSong").onClick((event) => {
 const idOfItem = event.context.itemId;
 let rowData;
 let userId = wixUsers.currentUser.id;
 let userphoto = itemData.mymarks[0].profilePhoto;
 let name = itemData.mymarks[0].fullName;

                            $w('#repeater1').data.forEach((item) => {
 if (item._id === idOfItem) {
                                    rowData = item;
                                } else {
 let toSave = {
 "mark": item,
 "userId": userId,
 "userPhoto": userphoto,
 "name": name
                                    };
                                    wixData.save("favourites", toSave);
                                }
                            });
                            console.log(rowData); // your item

                            $item("#saveSong").hide();
                            $item("#songSaved").show();
                            $item("#copiedBox").show();
                            $item("#InfoTextBox").label = "Added to Favourites";
                            setTimeout(function () {
                                    $item("#copiedBox").hide();
                                },
 3000); // <-- time in milliseconds 
                        })
                        $item("#songSaved").onClick(() => {
                            $item("#saveSong").show();
                            $item("#songSaved").hide();
                            $item("#copiedBox").show();
                            $item("#InfoTextBox").label = "Removed from Favourites";
                            setTimeout(function () {
                                    $item("#copiedBox").hide();
                                },
 3000); // <-- time in milliseconds 
                        })

```

Thanks in advance!

---

<div class="post-metadata">

**Author:** ![events32996](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/events32996/32/16198_2.png) [@events32996](https://forum.wixstudio.com/u/events32996)\
**Post date:** [December 9, 2020, 6:01pm UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990/2 "2020-12-09T18:01:05Z")

</div>

@ajithkrr Hey man! Wondered if you could assist at all please?

---

<div class="post-metadata">

**Author:** ![ajithkrr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ajithkrr/32/20520_2.png) [@ajithkrr](https://forum.wixstudio.com/u/ajithkrr)\
**Post date:** [December 10, 2020, 6:15am UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990/3 "2020-12-10T06:15:26Z")

</div>

I just create a trial site and let you know if it works !!

---

<div class="post-metadata">

**Author:** ![ajithkrr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ajithkrr/32/20520_2.png) [@ajithkrr](https://forum.wixstudio.com/u/ajithkrr)\
**Post date:** [December 10, 2020, 7:18am UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990/4 "2020-12-10T07:18:35Z")

</div>

Hi @events32996 !!!  
I just created a test site → [https://ajithkrr.wixsite.com/mysite/test-page](https://ajithkrr.wixsite.com/mysite/test-page)

Can you tell me if this is what you are trying to achieve…

---

<div class="post-metadata">

**Author:** ![events32996](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/events32996/32/16198_2.png) [@events32996](https://forum.wixstudio.com/u/events32996)\
**Post date:** [December 10, 2020, 1:52pm UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990/5 "2020-12-10T13:52:11Z")

</div>

This is why you’re the best! Haha

Yes that’s exactly what I’m looking to achieve!

---

<div class="post-metadata">

**Author:** ![ajithkrr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ajithkrr/32/20520_2.png) [@ajithkrr](https://forum.wixstudio.com/u/ajithkrr)\
**Post date:** [December 10, 2020, 2:36pm UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990/6 "2020-12-10T14:36:23Z")

</div>

Here is the code I used →   
You should connect the repeater to the marks **dataset** for this to function properly…

You can edit the code as you needs…

```javascript
import wixData from 'wix-data';
import wixUsers from 'wix-users';

let findArray = [];
$w.onReady(function () {

    $w('#repeater1').onItemReady(($item, itemData, index) => {
        wixData.query("favourites")
            .eq("userId", wixUsers.currentUser.id)
            .find()
            .then((results) => {
 let items = results.items;
 for (var i = 0; i < results.items.length; i++) {
 var array = items[i].mark; 
                    findArray.push(array);
                }
                check();
            });

        $item('#saveSong').onClick((event) => {
 let toInsert = {
 "mark": itemData._id,
 "userId": wixUsers.currentUser.id
            }
            wixData.insert("favourites", toInsert)
                .then(() => {
                    $w('#dataset1').refresh();
                    $item("#saveSong").hide();
                    $item("#savedSong").show();
                    $item("#copiedBox").show();
                    $item("#infoTextBox").text = "Added to Favourites";
                    setTimeout(function () {
                            $item("#copiedBox").hide();
                        },
 3000);
                });

        });

        $item('#savedSong').onClick((event) => {
            wixData.query("favourites")
                .eq("userId", wixUsers.currentUser.id)
                .eq("mark", itemData._id)
                .find()
                .then((res) => {
 let item = res.items[0];
 let toRemoveId = item._id

                    wixData.remove("favourites", toRemoveId)
                        .then(() => {
                            $w('#dataset1').refresh();
                            $item("#saveSong").show();
                            $item("#savedSong").hide();
                            $item("#copiedBox").show();
                            $item("#infoTextBox").text = "Removed from Favourites";
                            setTimeout(function () {
                                $item("#copiedBox").hide();
                            }, 3000);
                        });
                });
        });
    });
});

function check() {
    $w('#repeater1').forEachItem(($item, itemData, index) => {

 if (findArray.find(el => el === itemData._id) !== itemData._id) {
            $item('#saveSong').show();
            $item('#savedSong').hide();
        } else {
            $item('#saveSong').hide();
            $item('#savedSong').show();
        }
    });
}

```

Happy coding !!

---

<div class="post-metadata">

**Author:** ![events32996](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/events32996/32/16198_2.png) [@events32996](https://forum.wixstudio.com/u/events32996)\
**Post date:** [December 10, 2020, 3:08pm UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990/7 "2020-12-10T15:08:18Z")

</div>

@ajithkrr Thank you! I will give that a try now.

I am looking for a developer (paid) for my project, would this be of any interest to you?

---

<div class="post-metadata">

**Author:** ![ajithkrr](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ajithkrr/32/20520_2.png) [@ajithkrr](https://forum.wixstudio.com/u/ajithkrr)\
**Post date:** [December 10, 2020, 3:11pm UTC](https://forum.wixstudio.com/t/onclick-function-to-save-repeater-item-as-favourite/10990/8 "2020-12-10T15:11:54Z")

</div>

Actually I am due in other…  
Will let you know, when it finishes
