0

In my react application I have this button which acts like state switcher this button has an id attribute/prop and this button wraps a font-awesome icon.

<button id={feedId}>
   <i className={isStatus === 'Y' ? 'fa fa-unlock' : 'fa fa-lock'} /> 
   {isStatus === 'Y' ? 'Active' : 'Inactive'}
</button>

Now depending upon where you click on the button the event.target would return the entire button with id or just the icon that's <i> tag how do I make sure that entire button gets return as part of event.target instead of just <i>

4
  • What are you using event.target for? Commented Aug 22, 2018 at 17:28
  • 1
    Why, what do you ultimately want to do? Sounds like the XY problem. Commented Aug 22, 2018 at 17:28
  • 1
    Set up the event handler on the button and then check event.target to see if it is the button or not. Only proceed when it is the button. Because of bubbling, the button will receive the event all the time. Commented Aug 22, 2018 at 17:28
  • @ Hunter McMillen To get the id there are several such buttons in the application. id plays important part as it is use to make api calls Commented Aug 22, 2018 at 17:30

1 Answer 1

3

You can use currentTarget

function test(e) {
  console.log('Target ', e.target);
  console.log('Current Target ', e.currentTarget)
}
<link href="https://stackpath.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css" rel="stylesheet" />
<button id="test" onclick="test(event)">
       <i class="fa fa-quora" aria-hidden="true"></i>
       Text
    </button>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.