Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda