user8053092
user8053092

Reputation: 165

How to make disabled state of font awesome icon button?

I am using font awesome icon button and I need to have a disabled state of the icon. I am able to make it look like disabled button using Bootstrap disabled class but it's clickable. Is there any way to make is non-clickable ?

Here is my HTML code:--

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<button class='btn btn-primary disabled' disabled>
  <i class="fa fa-search"></i>
</button>

Upvotes: 4

Views: 15519

Answers (6)

zer00ne
zer00ne

Reputation: 43990

.focus(), trigger(), & .blur()

After clarifying what OP really wanted: ***to keep button.disabled from stealing focus from enabled buttons. Updated demo does that but not to button[disabled] those are truely dead to the world.💀

[disabled] Attribute Always Works

[disabled] attribute works. Bootstrap .disabled class does not. See the tests in demo below.

Demo

$('.btn').on('click', function(e) {
  console.log('clicked')
});


$('.btn').not('.disabled').on('click', function() {
  $('.btn').removeClass('lastFocus');
  $(this).addClass('lastFocus');
});

$('.disabled').on('focus', function(e) {
  $(this).blur();
  $('.lastFocus').trigger('focus');
});
b {
  color: cyan
}

u {
  color: black;
  font-weight: 700
}
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.2.1/css/bootstrap.min.css" />
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />

<button class='btn btn-primary' disabled>
  <i class="fa fa-search"></i><b>[disabled] attribute</b>
</button>

<button class='btn btn-primary disabled' disabled>
  <i class="fa fa-search"></i><b>[disabled] attribute</b> and <u>.disabled class</u>
</button>

<button class='btn btn-primary disabled'>
  <i class="fa fa-search"></i><u>.disabled class</u>
</button>

<button class='btn btn-primary disabled'>
  <u>.disabled class</u>
</button>

<button class='btn btn-primary'>
  <i class="fa fa-search"></i>enabled
</button>

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

Upvotes: 2

Daut
Daut

Reputation: 2625

You can also use onclick="return false;" to make sure it won't fire click event.

<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<button class='btn btn-primary disabled' onclick="return false;" disabled >
  <i class="fa fa-search"></i>
</button>

Upvotes: 0

user8053092
user8053092

Reputation: 165

It worked using pointer-events on icon.

Here is my answer:-

<button class='btn btn-primary disabled' disabled>
    <i class="fa fa-search" style="pointer-events:none"></i>
</button>

Upvotes: 0

Kandy
Kandy

Reputation: 673

You can try in this way also.

.xyz
        {
        cursor: not-allowed;
        pointer-events: none;
        color: #c0c0c0;
        background-color: #ffffff;
        }
 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>

<button class='btn btn-primary xyz'>
  <i class="fa fa-search"></i>
</button>

Upvotes: 0

ellipsis
ellipsis

Reputation: 12152

Use this

   

 <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
    <button class='btn btn-primary disabled' disabled style="pointer-events:none">
      <i class="fa fa-search" ></i>
    </button>

Upvotes: 0

use pointer-events: none;

button{
    pointer-events: none;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<button class='btn btn-primary disabled' disabled>
  <i class="fa fa-search"></i>
</button>

Upvotes: 6

Related Questions