Clear member's specific custom field data 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?
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
3 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 🦜
Please sign in to leave a comment.