Clear member's specific custom field data Answered

Post author
Al Mazlin

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

4 comments

  • Comment author
    Chukwudi Onyekwere

    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.

    0
  • Comment author
    Al Mazlin

    Thanks Chukwudi… that did the trick! 🥳

    0
  • Comment author
    Chukwudi Onyekwere

    You're welcome. Glad it worked 🦜

    1
  • Comment author
    jonny Van Marrum

    You are a legend! I've been trying to solve this problem for 2 days 🙌

    0

Please sign in to leave a comment.