Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable a link in the vue component?

My html like this :

<div id="app">
    <a class="btn btn-danger" href="javascript:" @click="add($event)">add</a>
</div>

My javascript like this :

var vue = new Vue({
    el: '#app',

    methods: {
        add(event) {
            event.target.disabled = true
        }
    }
});

Demo and full code like this : https://jsfiddle.net/q7xcbuxd/221/

I try like that. But if I click button add, it's not disabled

How can I solve this problem?

like image 750
moses toh Avatar asked Mar 27 '18 03:03

moses toh


People also ask

How do I turn off Vue link?

Add an event to your element and preventDefault. Then, add a custom css class that would grayed out the button and with disabled mouse cursor, and bind that class to your element.

How do I disable links?

To remove a hyperlink but keep the text, right-click the hyperlink and click Remove Hyperlink. To remove the hyperlink completely, select it and then press Delete.

How do I disable a link tag?

The best way to disable a link tag is by using the CSS property pointer-events: none; . When you apply pointer-events: none; to any element, all click events will be disabled. Because it applies to any element, you can use it to disable an anchor tag.

How do you disable a link in CSS?

Answer: Use the CSS pointer-events Property You can simply use the CSS pointer-events property to disable a link. The none value of this property specify the element is never the target of pointer events.


3 Answers

Since you are using boostrap, the proper way to disable a (anchor) button is not to set .disabled = true, but to add a disabled class.

Two other notes. You probably want to prevent the default behavior of the click event, so use @click.prevent. Also, if you don't have additional arguments, you don't need to use ="add($event)", just ="add" will suffice.

Demo below:

new Vue({
  el: '#app',
  methods: {
    add(event) {
      event.target.className += ' disabled'
    }
  }
})
body { padding: 10px }
<script src="https://unpkg.com/vue"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<div id="app">
  <a class="btn btn-danger" href="javascript:" @click.prevent="add">add</a>
</div>

You can also go pure Vue and use a class binding:

new Vue({
  el: '#app',
  data: {
    btnDisabled: false
  },
  methods: {
    add(event) {
      this.btnDisabled = true; // mutate data and let vue disable the element
    }
  }
})
body { padding: 10px }
<script src="https://unpkg.com/vue"></script>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">

<div id="app">
  <a class="btn btn-danger" href="javascript:" @click.prevent="add" :class="{disabled: btnDisabled}">add</a>
</div>
like image 53
acdcjunior Avatar answered Oct 10 '22 17:10

acdcjunior


Add an event to your element and preventDefault.

Then, add a custom css class that would grayed out the button and with disabled mouse cursor, and bind that class to your element.

CSS:

.disabled {
  cursor: not-allowed;
  color: gray
}

HTML:

<a href=""  @click.prevent="add" :class="disabledClass" >Add</a>

JS:

computed: {
  disabledClass: () => {
    return isAddButtonDisabled ? "disabled" : ""
  }
}
like image 23
Elpedio Jr. Adoptante Avatar answered Oct 10 '22 17:10

Elpedio Jr. Adoptante


event.preventDefault() would disable it.

and .prevent modifier allows you to add it easily

@click.prevent="add($event)"

like image 2
Jacob Goh Avatar answered Oct 10 '22 19:10

Jacob Goh