51

I created a button that open a modal window on click.

<a href="#"  data-toggle="modal" data-target="#myModal" class="signup-button gray-btn pl-pr-36" id="connectBtn"  data-role="disabled">Connect</a>

For some reason the data-role="disabled" doesn't work good. How can I disable it?

sarit rotshild
  • 733
  • 2
  • 10
  • 19

4 Answers4

88

You can use CSS to accomplish this:

.disabled {
  pointer-events: none;
  cursor: default;
}
<a href="somelink.html" class="disabled">Some link</a>

Or you can use JavaScript to prevent the default action like this:

$('.disabled').click(function(e){
    e.preventDefault();
})
Penny Liu
  • 15,447
  • 5
  • 79
  • 98
iivannov
  • 4,341
  • 1
  • 18
  • 24
  • 6
    Good idea, but this won't disable it. You can still use the tab to focus the link, and press enter to follow it. – Oriol Feb 04 '15 at 09:43
9

I created a button...

This is where you've gone wrong. You haven't created a button, you've created an anchor element. If you had used a button element instead, you wouldn't have this problem:

<button type="button" data-toggle="modal" data-target="#myModal" data-role="disabled">
    Connect
</button>

If you are going to continue using an a element instead, at the very least you should give it a role attribute set to "button" and drop the href attribute altogether:

<a role="button" ...>

Once you've done that you can introduce a piece of JavaScript which calls event.preventDefault() - here with event being your click event.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
4
.disabledLink.disabled {pointer-events:none;}

That should do it hope I helped!

ekad
  • 14,436
  • 26
  • 44
  • 46
Grant Hood
  • 71
  • 1
  • 6
-2
<script>
    $(document).ready(function(){
        $('#connectBtn').click(function(e){
            e.preventDefault();
        })
    });
</script>

This will prevent the default action.

James Donnelly
  • 126,410
  • 34
  • 208
  • 218
Sameer Shaikh
  • 7,564
  • 2
  • 17
  • 32
  • 9
    Though very likely, considering jQuery isn't tagged you shouldn't post answers until further clarification is given. Your first approach here should have been to leave a comment asking *Are you using jQuery?* And, considering you didn't, what you should have definitely done is preface your answer with an *If you are using jQuery...*. – James Donnelly Feb 04 '15 at 09:42