Warm tip: This article is reproduced from serverfault.com, please click

Electron: Check box not changing value?

发布于 2020-11-28 20:44:56

I've created a browser window that uses a check box to open and close itself however, only the checkbox in its true state is being detected and not in its false state.

Here is my code in the renderer.js:

const {width, height} = screen.getPrimaryDisplay().workAreaSize;
let close = document.querySelector('input[name=open_close]');
close.addEventListener('change', function(event){
  if (this.checked = true) {
  console.log("renderer");
 ipcRenderer.send('expClose',width, height);
  }

  if(this.checked = false) {
 ipcRenderer.send('expOpen',width - 500, height - 450);
  }
});

And here is my my main.js:

ipcMain.on('expClose', function(e,width, height) {
    console.log('closed'); 
      mainWindow.setPosition(width - 75, height - 450);
  });
  
  ipcMain.on('expOpen', function(e,width, height) {
   console.log("opened")
   mainWindow.setPosition(width, height);
      
  });

Also my HTML if needed:

<input type="checkbox" name="setbtn" checked="false" id="settings" class="btnset">

I assumed that the false state isn't being detected because opened isn't being logged. Any help will be greatly appreciated.

Questioner
HaloDonuts J
Viewed
0
Gabriele Petrioli 2020-11-29 05:19:39

Your using = (assignment) instead of === (comparison)

Change if (this.checked = true) { to if (this.checked === true) {
and if(this.checked = false) { to if(this.checked === false) {