Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

title toggle disappears on click

Trying to make a toggle work that open/closes content. Opening the content works but somehow the button with the title disappears. Why?

<script>
const show = ref(false);
</script>

<template>
  <div class="w-full md:w-1/2 mx-auto my-12 border-8 border-black">
    <button v-if="!show" class="bg-blue-500" @click="show = true">
      <h2>Hello title, click to open or close content</h2>
    </button>
    <div v-show="show" class="bg-red-500">
      <p>The content becomes available when you click to open, and is hidden when clicked again to close.
    </div>
  </div>
</template>

Why the title disappears and how to fix that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The title disappears when you click it because the v-if expression, !show, evaluates to false and thus the element would be removed from the DOM. Consider removing the v-if attribute from the <button> element if you would like it to always be displayed.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading