disable a link

0

I want to disable this link for a few seconds.

<a href="#" data-role="button" data-theme="f" data-rel="popup" id="deshabilitar" type="button" data-icon="cloud-upload" data-iconshadow="true" onclick="deshabilitar()"></a>

so that it only allows to click once. In the function I have this code but it does not do it to me and I do not see why. Thanks.

 var button = $('#deshabilitar');
button.attr('disabled', 'disabled');
setTimeout(function() {
     button.removeAttr('disabled');
},3000);

link

    
asked by tripossi 13.09.2017 в 12:15
source

4 answers

1

What you could do is assign the link the property pointer-events of css with value none by javascript and in a few seconds then remove that property so that it is enabled again.

Here is an example:

This would be the HTML :

<a href="#" onclick="deshabilitar(this)">Dame click</a>

And this javascript :

function deshabilitar( link ){
    link.style.pointerEvents = 'none';
    link.style.color = '#bbb';

    setTimeout(function(){
        link.style.pointerEvents = null;
        link.style.color = 'blue';
    }, 3000);
}

I've tried it and it works, I hope and it serves you.

    
answered by 15.09.2017 / 04:17
source
1

Maybe you did not have the reference well, try:

function deshabilitar() {
    $(this).attr('disabled', 'disabled');
    setTimeout(function() {
        $(this).removeAttr('disabled');
    }, 3000);
}
    
answered by 13.09.2017 в 12:22
1

I think the problem is that you can not add the disabled attribute to a link. In this link (in English) you have information about which is the best way to disable it

    
answered by 13.09.2017 в 12:32
1

A link is not a form control so you can disable it and avoid the default behavior.

Try adding a status of enabled / disabled to the link and according to the status of the link, you do the corresponding action.

Here is an example:

$('#deshabilitar').click(function(){
   var $this = $(this)
   
   if($this.hasClass('inhabilitado')) {
     alert("link unhabilitado");
   }
   
    $this.addClass('inhabilitado');
    setTimeout(function() {
        $this.removeClass('inhabilitado');
        alert("link habilitado")
    }, 3000);
});
.inhabilitado{
  color:#ccc;
  cursor:default;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a href="#" data-role="button" data-theme="f" data-rel="popup" id="deshabilitar" type="button" data-iconshadow="true">link</a>

What it does is simple, add a class called deshabilitado when you click on item. This class changes the color and the cursor to give the impression that it is disabled. Then after the elapsed time the styles are removed and the link is enabled again.

    
answered by 13.09.2017 в 14:26