0

I have a close font-awesome icon inside a button like so:

<button type="button" class="btn btn-sm btn-primary">
    Feature A <i class="close-item fas fa-times"></i>
</button>

And since it is dynamically added to DOM I'm listening to click events on the icon like so:

$(document).on('click', '.close-item', function() {
    console.log("Clicked!");
});

The goal is to listen to click events on just the icon (not the rest of the button). It works perfectly on Google Chrome but not in Firefox. How can I make this work on Firefox?

Thanks in advance.

random425
  • 679
  • 1
  • 9
  • 20

4 Answers4

1

A click event within a button does not work in Firefox, you can just change the button to an a tag like this:

<a href="#" class="btn btn-sm btn-primary">
    Feature A <i class="close-item fas fa-times"></i>
</a>

or

<a href="javascript:;" class="btn btn-sm btn-primary">
    Feature A <i class="close-item fas fa-times"></i>
</a>
mylee
  • 1,293
  • 1
  • 9
  • 14
0

You could use .parent() to select the button surrounding the icon:

$('.close-item').parent().click( function() {
    console.log("Clicked!");
});
Bruno Robert
  • 302
  • 2
  • 8
  • Want to list to click events just on the icon, not the rest of the button (updated the question). Thks for your reply. – random425 Feb 20 '19 at 02:35
0

Use fa class not fas

<button type="button" class="btn btn-sm btn-primary"> Feature A <i class="close-item fa fa-times"></i> </button>

Here is working example

<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet"/>
<br><br>
<i class="fa fa-calendar-plus-o"></i> <br><br>
<i class="fa fa-user-circle-o"></i>
<br><br>
<i class="fa fa-pencil-square-o"></i>
Daniel Smith
  • 1,626
  • 3
  • 29
  • 59
  • The fas tag is font awesome 5. Basically, you just downgraded the OP’s fontawesome version as a fix. – Nate Feb 20 '19 at 02:38
0

Try sending event object to function

$('.close-item').on('click', function(event e) {
    console.log("Clicked!");
});

Reffer questions: JavaScript onClick does not work in Firefox jQuery click function not working on firefox

DerekN
  • 1
  • 2