How to clear specific custom field data from members with a button click? Answered
Clearing custom fields: Sorry if this is documented somewhere, but is there a way to clear specific custom field data from a member, ideally either with a button click or on specific page load?
Comments
6 comments
Hi Aletta,
Code 1 below will clear the custom field 'myField' when a button with id 'myButton' is clicked while Code 2 will clear 'myField' when a page loads.
CODE 1:
<script>document.addEventListener("DOMContentLoaded", async function() {const memberstack = window.$memberstackDom;memberstack.getCurrentMember().then(async ({ data: member }) => {if (member) {if (member["customFields"]) {// adds event listener to button with the myButton id.document.getElementById("myButton").addEventListener("click", async function () {try {await memberstack.updateMember({customFields: {"myField": ""}});console.log("myField cleared.");} catch (error) {console.error('Error clearing myField:', error);}});}}});});</script>CODE 2:
<script>document.addEventListener("DOMContentLoaded", async function() {const memberstack = window.$memberstackDom;memberstack.getCurrentMember().then(async ({ data: member }) => {if (member) {try {await memberstack.updateMember({customFields: {"myField": ""}});console.log("myField cleared on page load.");} catch (error) {console.error('Error clearing myField on page load:', error);}}});});</script>Make sure to place either of these scripts within your HTML file, preferably just before the closing </body> tag. Also, do not forget to replace myField with your custom field ID and myButton with the ID of the button on your website.
I hope this helps.
Thanks Chukwudi… that did the trick! 🥳
You're welcome. Glad it worked 🦜
You are a legend! I've been trying to solve this problem for 2 days 🙌
Where can I delete custom fields that I added to members?
You can delete custom fields in the members panel of Memberstack admin. Look for the custom field and click edit. Then, it will show up a modal where you can delete the selected field
Please sign in to leave a comment.