1

Need to add close button in default Magento messages(success,error,notice). I have tried to add it in messages.phtml but nothing happens.

Please help!

1
  • can you please share some visual look where you need them is it a popup?are you using magento popup?
    – Asad Ullah
    Commented Oct 4, 2021 at 20:56

2 Answers 2

2

Below is the code and it's working fine.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
?>
<div data-bind="scope: 'messages'" id="Myid" >
    <!-- ko if: cookieMessages && cookieMessages.length > 0 -->
    <div role="alert" data-bind="foreach: { data: cookieMessages, as: 'message' }" class="messages">
        <div data-bind="attr: {
            class: 'message-' + message.type + ' ' + message.type + ' message',
            'data-ui-id': 'message-' + message.type
        }">
            <div data-bind="html: message.text"></div>
            <span id='close' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;' class="customClose">x</span> 
        </div>
          
    </div>

    <!-- /ko -->
    <!-- ko if: messages().messages && messages().messages.length > 0 -->
    <div role="alert" data-bind="foreach: { data: messages().messages, as: 'message' }" class="messages">
        <div data-bind="attr: {
            class: 'message-' + message.type + ' ' + message.type + ' message',
            'data-ui-id': 'message-' + message.type
        }">
            <div data-bind="html: message.text"></div>
             <span id='addtocartclose' onclick='this.parentNode.parentNode.parentNode.removeChild(this.parentNode.parentNode); return false;' class="customClose">x</span> 
        </div>

    </div>
    <!-- /ko -->

</div>
<script type="text/x-magento-init">
    {
        "*": {
            "Magento_Ui/js/core/app": {
                "components": {
                        "messages": {
                            "component": "Magento_Theme/js/view/messages"
                        }
                    }
                }
            }
    }
</script>

<script>
function toggleText(){
  var x = document.getElementById("Myid");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
</script>
<style type="text/css">
    .alert .message .customClose{
    display: block;
}

.alert .customClose{
    display: none;
}
</style>

3
  • Where we can add that? Commented Jan 4, 2022 at 9:18
  • 1
    you can add app/design/frontend/[Vendor]/[theme]/Magento_Theme/templates/messages.phtml file Commented Jan 4, 2022 at 9:26
  • @SweetyMasmiya thanks mate. For M2.4.4 this only works once, and the messages won't appear again unless the page is refreshed. This works multiple times <span id='addtocartclose' onclick='this.parentNode.parentNode.removeChild(this.parentNode); return false;' class="customClose">x</span> Commented Jun 14, 2022 at 3:17
0

Please use the blog for refrence to hide the messages

https://www.interactiv4.com/blog-es-en/automatically-hide-messages-in-magento-2/?lang=en

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.