1

My html code like this :

<div class="chatbot chatbot--closed ">
  <div class="chatbot__header">
    <p><strong>Got a question?</strong> <span class="u-text-highlight">Ask Harry</span></p>
    <svg class="chatbot__close-button icon-speech" viewBox="0 0 32 32">
      <use xlink:href="#icon-speech" />
    </svg>
    <svg class="chatbot__close-button icon-close" viewBox="0 0 32 32">
      <use xlink:href="#icon-close"  data-toggle="modal" data-target="#basicModal" />
    </svg>
  </div>
  <div class="chatbot__message-window">
    <ul class="chatbot__messages">
      <li class="is-ai animation">
        <div class="is-ai__profile-picture">
          <svg class="icon-avatar" viewBox="0 0 32 32">
            <use xlink:href="#avatar" />
          </svg>
        </div>
        <span class="chatbot__arrow chatbot__arrow--left"></span>
        <p class='chatbot__message'><strong class='intro'>Hello, I’m Harry, your virtual assistant. I'm here to help with your general enquiries.</strong>Example of questions you can ask for demo purpose: <br><em>Hi / How are you? / I'd like some financial advice / Can you email me a statement? / Can I get a form? /  How do I cancel my life insurance? </em></p>
      </li>
      <!-- Message here -->
    </ul>
  </div>
  <div class="chatbot__entry chatbot--closed">
    <input type="text" class="chatbot__input" placeholder="Write a message..." />
    <svg class="chatbot__submit" viewBox="0 0 32 32">
      <use xlink:href="#icon-send" />
    </svg>
  </div>
</div>

Demo and full code like this : https://codepen.io/trendingnews/pen/wvBorrR?editors=1010

When the user clicks close on the chatbot, it will appear a feedback modal to fill the user experience using chatbot. If the user clicks the close icon on the chatbot, it will close the chatbot. I want the chatbot can closed too when the user clicks the element outside the chatbot

How can I do it?

moses toh
  • 12,344
  • 71
  • 243
  • 443
  • 1
    there is a similar question already. Does this help you? https://stackoverflow.com/a/153047/12545040 – BrOsCoRe Dec 17 '19 at 09:12
  • @BrOsCoRe okay. this : `$(window).click(function() { //Hide the menus if visible });`. Right? But how do I run that statement when the chatbot opens? So if the chatbot is closed, it doesn't run the statement – moses toh Dec 17 '19 at 09:32

1 Answers1

1

Try this:

CodePen


$document.addEventListener("click", function(event) { 
  $target = $(event.target);
  var element = document.getElementsByClassName("chatbot");
  if(!$target.closest('.chatbot').length && !$target.closest('#chat-circle').length && (element[0].style.display == "block")) {
      element[0].style.display = "none";
      document.getElementById("chat-circle").style.display="block";
  }        
});

Maybe it is not the best solution but it worked for me.

BrOsCoRe
  • 73
  • 1
  • 10