Button click event binding in Electron js

 

sourabh

Button click event binding in Electron js

In Electron, you can bind an event listener to a button click using JavaScript. Here is an example of how to do it:

const { BrowserWindow } = require('electron'); let win = new BrowserWindow({ /* window options */ }); win.webContents.on('did-finish-load', () => { win.webContents.executeJavaScript(` document.querySelector('button').addEventListener('click', function() { console.log('Button clicked'); }); `); });

In this example, the executeJavaScript() method is used to execute a JavaScript function that adds an event listener to the button. The event listener is triggered when the button is clicked, and it logs a message to the console.

You can also bind event to the button using the addEventListener() method inside the renderer process.

const button = document.querySelector('button'); button.addEventListener('click', function() { console.log('Button clicked'); });

You can also use jQuery to bind the click event, like this:

const { BrowserWindow } = require('electron'); let win = new BrowserWindow({ /* window options */ }); win.webContents.on('did-finish-load', () => { win.webContents.executeJavaScript(` $('button').click(function() { console.log('Button clicked'); }); `); });

Please note that the above examples use console.log() function to log the message, you can replace it with your desired functionality.






Post a Comment

0 Comments