简体   繁体   中英

Font-Awesome: Can I make icon clickable?

I'm using a trash can icon in a react-table row, and I want to delete the row when the icon is clicked (my knowledge of HTML/JS is very basic). But when I put an onClick handler either in or around the icon, it fires as the page is rendered, and not at all when the icon is clicked.

Here is my current row definition. I've tried div and span , and I tried using a button , but the icon didn't display correctly:

{
  width: 50,
  filterable: false,
  Cell: row => (
  <div align="center" onClick={alert("clicked")}>
    <i className="fa fa-trash-o"></i>
  </div>
  )
}

Can someone please tell me what I'm doing wrong?

React event bindings do not work without an actual function wrapper.

Try it as onClick={()=>{alert('clicked')}}

See also: these docs .

You certainly can make icons clickable. You could go one of two routes I can think of atm..

You can either surround the icon in an 'a' tag or you could use javascript to grab the element (probably getElementById) and add a click listener to it, with an easy function attached that executes your hearts desire.

Hope that helps

You don't need the brackets.

You can just have the function as alert('clicked') or you can define a function inside of your JavaScript and link to it in the onclick of the span.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM