How do I display code on thank you page based on filled metafield?

Topic summary

A user is trying to conditionally display custom metafield content on Shopify’s thank you page using JavaScript in the checkout settings’ additional scripts section.

Current Problem:

  • The code hides the section when the metafield is blank (desired behavior)
  • However, it fails to display the content when the metafield is actually filled in (unintended)
  • The metafield in question is a multi-line text type: order.metafields.custom.message_to_customer

Goal:
Only show the “Delivery message” section and its content when the metafield contains data; hide it completely when blank.

Status:
Another user suggested a conditional approach, but the initial attempt didn’t resolve the display issue when content is present. The discussion remains ongoing as they troubleshoot why filled metafields aren’t appearing despite the blank state being handled correctly.

Summarized with AI on November 13. AI used: claude-sonnet-4-5-20250929.

@SomeUsernameHe Thank you for your fast response!

I have just tried that and it removes the section when blank. But doesn’t seem to re-appear when content is added.

If you have any more suggestions that would be greatly appreciated.

Thanks