0

I want to make a conditional p tag that appears only if a button is disabled I currently have this:

  <div class="finishTest">
    <button
      form="answerForm"
      type="submit"
      @click="getResult"
      :disabled="!this.enableButton()"
      :class="{
        disabledButton: !this.enableButton(),
      }"
    >
      {{ t("finish") }}
    </button>
    <p v-if="button == disabledButton">error</p>
  </div>

but this shows the p tag all the time.

2 Answers 2

3

Don't confuse with the style class variable. You can easily achieve this with !enableButton.

Try <p v-if="!enableButton">error</p> instead of <p v-if="button == disabledButton">error</p>

Demo :

new Vue({
  el: '.finishTest',
  data: {
    enableButton: true
  },
  methods: {
    getResult() {
        this.enableButton = false
    }
  }
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div class="finishTest">
  <button
          type="submit"
          @click="getResult"
          :disabled="!enableButton"
          >
    Click Me
  </button>
  <p v-if="!enableButton">error</p>
</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Within this code line you use button and disabledButton.
Where did you define button and disabledButton ?

 <p v-if="button == disabledButton">error</p>

2 Comments

I didnt define them I just wrote is as an example to try and show what im trying to achieve.
The answer below is the correct answer on how to do this. Try not to complicate things too much.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.