{"id":7861,"date":"2025-02-04T09:42:18","date_gmt":"2025-02-04T09:42:18","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/?p=7861"},"modified":"2025-10-03T15:56:56","modified_gmt":"2025-10-03T10:26:56","slug":"how-to-display-messages-in-hyva-theme-using-js","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/how-to-display-messages-in-hyva-theme-using-js\/","title":{"rendered":"How to Display Messages in Hyv\u00e4 Theme Using JS?"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In this blog post, I will show you how to display messages in Hyv\u00e4 theme using JS.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Hyv\u00e4 provides a global function <strong>window.dispatchMessages()<\/strong> to show messages.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">You can use this to display various types of messages on the page through JS like:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Success:<\/strong> Indicates a successful operation<\/li>\n\n\n\n<li><strong>Notice:<\/strong> Provides important information or updates<\/li>\n\n\n\n<li><strong>Warning:<\/strong> Alerts users about potential issues<\/li>\n\n\n\n<li><strong>Error:<\/strong> Informs users about errors or unsuccessful operations<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s an example JS syntax of the dispatch message array for the Hyv\u00e4 theme:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">window.dispatchMessages([\n   {\n       type: \"success\",\n       text: \"Great job! Your profile has been updated successfully.\"\n   }\n], 2000);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In the above code, the <strong>type <\/strong>attribute defines the message type and the <strong>text<\/strong> attribute contains the actual message to be shown. Optionally, you can also define the message duration in milliseconds (ms).<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The above code will display the following message:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1527\" height=\"204\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hyva-theme-message-example-using-js.png\" alt=\"Hyva message using JS example\" class=\"wp-image-7933\" srcset=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hyva-theme-message-example-using-js.png 1527w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hyva-theme-message-example-using-js-250x33.png 250w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hyva-theme-message-example-using-js-700x94.png 700w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hyva-theme-message-example-using-js-768x103.png 768w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hyva-theme-message-example-using-js-403x54.png 403w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hyva-theme-message-example-using-js-964x129.png 964w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hyva-theme-message-example-using-js-120x16.png 120w\" sizes=\"auto, (max-width: 1527px) 100vw, 1527px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">You can trigger the message dynamically using JavaScript in specific conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using JS to Display Messages on Hyv\u00e4 Based on Conditions<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The dispatchMessage global function can be used site-wide to show messages. You can use it to display messages, errors, and warnings on the front end based on triggers and events.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Let\u2019s understand it through an example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Hyv\u00e4 Message Example<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">You can use the success message type in Hyv\u00e4 to display a success message on the page. This can be useful for events like successful form submission and order placement.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, here\u2019s a Javascript notifications Hyv\u00e4 theme code that displays a success message on form submission:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">document.querySelector(\"#submit-button\").addEventListener(\"click\", function() {\n    \/\/ Simulating successful form submission\n    window.dispatchMessages([\n        {\n            type: \"success\",\n            text: \"Your form has been submitted successfully!\"\n        }\n    ], 3000); \/\/ Message disappears after 3 seconds\n});<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here\u2019s what it looks like:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"822\" height=\"116\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hvya-theme-message-example.png\" alt=\"Hyva success message example\" class=\"wp-image-7935\" srcset=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hvya-theme-message-example.png 822w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hvya-theme-message-example-250x35.png 250w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hvya-theme-message-example-700x99.png 700w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hvya-theme-message-example-768x108.png 768w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hvya-theme-message-example-403x57.png 403w, https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2025\/02\/hvya-theme-message-example-120x17.png 120w\" sizes=\"auto, (max-width: 822px) 100vw, 822px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Likewise, you can display notice, warning, and error messages in Hyv\u00e4 by implementing a similar JavaScript. And also use different <a href=\"https:\/\/meetanshi.com\/blog\/useful-hyva-javascript-helper-functions\/\" target=\"_blank\" rel=\"noreferrer noopener\">Hyv\u00e4 JavaScript helper functions<\/a> to make your storefront more accessible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">FAQs<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Can I Customize the Appearance \/ Colors of Hyv\u00e4 Messages?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes, you can customize the appearance of Hyv\u00e4 theme messages. To do so, you can override the default styles applied in the Tailwind CSS file.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do Hyv\u00e4 Messages Support HTML Formatting?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">No, you can not use HTML content inside Hyva&#8217;s messages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How Do I Remove a Message Before its Timeout?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">There are many ways to do this &#8212; you can use PHP to remove messages from the session or <\/p>\n\n\n\n<script type=\"application\/ld+json\">\n{\n  \"@context\": \"https:\/\/schema.org\",\n  \"@type\": \"FAQPage\",\n  \"mainEntity\": [\n    {\n      \"@type\": \"Question\",\n      \"name\": \"Can I Customize the Appearance \/ Colors of Hyv\u00e4 Messages?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"Yes, you can customize the appearance of Hyv\u00e4 theme messages. To do so, you can override the default styles applied in the Tailwind CSS file.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"Do Hyv\u00e4 Messages Support HTML Formatting?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"No, you cannot use HTML content inside Hyv\u00e4's messages.\"\n      }\n    },\n    {\n      \"@type\": \"Question\",\n      \"name\": \"How Do I Remove a Message Before its Timeout?\",\n      \"acceptedAnswer\": {\n        \"@type\": \"Answer\",\n        \"text\": \"There are many ways to do this -- you can use PHP to remove messages from the session or get help from the experts.\"\n      }\n    }\n  ]\n}\n<\/script>\n\n\n\n\n<h2 class=\"wp-block-heading\">Need Help? Consult the Hyv\u00e4 Experts<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Meetanshi is a leading e-commerce development company offering expert Magento and Shopify development solutions. We have an in-house team of certified Magento developers that can help you easily implement a fresh Magento 2 Hyv\u00e4 theme or convert your existing store theme to Hyv\u00e4.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Get started with <a href=\"https:\/\/meetanshi.com\/hyva-theme-development-service\">Hyv\u00e4 theme development <\/a>today! <\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog post, I will show you how to display messages in Hyv\u00e4 theme using JS. Hyv\u00e4 provides a global function window.dispatchMessages() to show&#8230;<\/p>\n","protected":false},"author":5,"featured_media":7866,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34,5869],"tags":[],"class_list":["post-7861","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-magento","category-hyva-themes"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/7861","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=7861"}],"version-history":[{"count":8,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/7861\/revisions"}],"predecessor-version":[{"id":23167,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/7861\/revisions\/23167"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media\/7866"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=7861"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=7861"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=7861"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}