0

so i have a form component having button array, though i used icons in it with value attribute. just like below.

<button value={'admin'} onClick={(e)=> handleClick(e.target.value)}>
      <FaUserSecret classname="text-lg" />
</button>

in handleClick function e.target.value is returning icon instead of admin.

Thanks in advance .

I tried to pass e as parametter and use its value in function but still same.

3 Answers3

0

You can use e.currentTarget.getAttribute("value") method to get the admin value assigned to the button.

CodeSandbox code reference

Difference b/w e.target and e.currentTarget - difference-between-e-target-and-e-currenttarget

warl0ck
  • 3,356
  • 4
  • 27
  • 57
  • Hi @warl0ck sry for making it complicated. its working `but` ***it need text inside button not just icon or else it returns null value*** [updated sandbox](https://codesandbox.io/s/floral-sky-g49tj0?file=/src/App.tsx) – Yogesh Kadu Feb 14 '23 at 17:30
  • you can get the value from `e.currentTarget` see the SO question mentioned in the updated answer – warl0ck Feb 15 '23 at 06:15
0

Can you please elaborate or share more code with us. Cause I tried your given code and it's working fine. I am getting 'admin' as the output.

    <div className='App'>
      <button value={'admin'} onClick={(e)=> console.log(e.target.value)}>
     Click
      </button>
    </div>
  • Hi @blackwatch021 the issue is that i used icons in button body instead of plane text as well it is treating that icon as button value not `value={'admin'}` attribute so **getting null value instead of admin** – Yogesh Kadu Feb 14 '23 at 17:35
0

Try this:

<button value={'admin'} onClick={e => handleClick(e.target.value)}>
      <FaUserSecret classname="text-lg" />
</button>

You're not supposed to use parenthesis around "e".

devill_h
  • 83
  • 8