So Im trying to get the value from a button. This my button:
<button
className="toggle__btn"
value={el._id}
onClick={toggle}
>
<i className="fa-solid fa-circle-info fs-4"></i>
</button>
This my function:
const toggle = (event) => {
const id = event.target.value;
console.log(id);
};
The problem is I can’t get the value if I click the icon, but I can when I click outside the icon but still inside the button(there is blank space outside the icon). I want it to return the id even when the icon is clicked. How to do so? Why does this happen?
4
Answers
Unlike
target
,currentTarget
remains the same throughout the event propagation, even if the event bubbles up to a parent element. It always refers to the element that the event listener was attached to.You can try using
currentTarget
to refer the element that attached the event. This will retrieve theid
from the button element, whether you click on the icon or the blank space inside the button.try
See https://medium.com/@etherealm/currenttarget-vs-target-in-js-2f3fd3a543e5
Try disabling pointer events on the child:
in the css file:
Please use
currentTarget