Masih Jahangiri
Masih Jahangiri

Reputation: 10927

Action on blur except when specific element clicked with react OR how to get clicked element onBlur

I want to prevent onBlur event when I click on a specific element:

<input type="text" onBlur={<call when I click outside of element except when I click on a specific element>} />

Upvotes: 6

Views: 2474

Answers (1)

dmgp
dmgp

Reputation: 391

What you could do is give the element that would stop the call an ID and then check for that ID via event.relatedTarget.id

    const doSomething = (event) => {
        if(event.relatedTarget.id === "badButton"){
            return
        }
        //Do things
    }

    return(
        <div className="DashboardHeader" onBlur={(e) => doSomething(e)}>
            <button id="badButton">newbutton</button>
        </div>
    )

Upvotes: 6

Related Questions