- Forums
- react
- React/Ionic Auto Click Button - Trigger Click Programmatically Automatically Without Human Click
use this code to auto trigger a click even for a button in react. Same as using trigger() function in jquery [5119], Last Updated: Mon Jun 24, 2024
dd
Wed May 10, 2023
0 Comments
1187 Visits
Use this code to auto trigger a click even for a button in react with Ionic Typescript application. Same as using $('#element').trigger('click') function in jquery but without a library or package, just typescript javascript.
import React, { useRef } from "react";
Resource: https://www.webune.com/forums/react-useref-my-notes-how-to-use-useref-with-examples.html
const refButton2= useRef(null);
or
const refButton2= useRef<HTMLInputElement>(null); // TYPESCRIPT
function showAlert(event:any) {
alert('button 2 clicked message');
}
function clickButton1(event:any) {
// trigger click on button 2 to show alert
refButton2.current.click();
}
Render() Section:
<IonButton onClick={clickButton1}>1. Trigger ShowAlert Button</IonButton>
<IonButton ref={refButton2} onClick={showAlert}>2. ShowAlert Button</IonButton>
v=8WtZ8fLw7SY