Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

123
Views
Button's onclick records the i tag instead of the button itself

I have a button and I want to capture its 'id' when it is clicked.

<button id={props.id} 
   onClick={props.handleClick} 
   className="btn btn-circle" >
   <i class="fas fa-angle-down"></i>
</button >

When I log the event console.log(event.target.id); in my handleClick function, it returns the i tag's 'id' (which is just an empty string) if I click in the middle of the button. If I click on the sides of the button, then my handleClick function works as intended.

This is what the button looks like for reference enter image description here

How should I fix this? I suppose I can add the same 'id' attribute to the i tag, but was wondering if there was a better fix.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could use currentTarget instead:

console.log(event.currentTarget.id);

This will return the currentTarget that you have attached your onClick function on, not the target that triggers the event.

about 4 years ago · Juan Pablo Isaza Report

0

you also can use this way to get id value

import { render } from "react-dom";
import React, { Component } from "react";
import "./styles.css";

class Click extends Component {
  constructor(props) {
    super(props);
  }

  click(e) {
    let id = e.target.attributes.getNamedItem("id").value;
    alert(id);
  }

  render() {
    return (
      <button onClick={this.click} id="This is id">
        Click Here
      </button>
    );
  }
}
export default Click;

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!