How do I access the DOM elements in the Electron?

Asked

Viewed 169 times

0

I’m new to Electron, and I’m trying to do a function from a click on a menu. Follow my example below.

index.html

<!DOCTYPE html>
<html lang="pt-br" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Electron</title>
    <script src="main.js"></script>
  </head>
  <body>
    <input type="text" name="campo" id="campo" value="">
    <button type="button" name="funcao" onclick="funcao()">Função</button> <br /><br />
    <input type="text" name="url" id="url">
  </body>
</html>

In this example, when typing something into the input "url" and click on the button, will appear what was typed in the input "campo". What I wanted is to do the same but by clicking on the menu "Função".

main.js (only in the desired part, which would be the part of the menu with the function just below)

  {
    label: 'Função',
    click () { funcao(); }
  },

function funcao() {
  document.getElementById("campo").value = document.getElementById("url").value;
}

The mistake is:

"Referenceerror: Document is not defined"

1 answer

0

take a look at the code below:

<html lang="pt-br" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Electron</title>    
  </head>
  <body>  
    <input type="text" name="campo" id="campo" value="">
    <button type="button" id="demo" onclick="funcao()">Função</button><br/><br/>
    <input type="text" name="url" id="url">

    <script>
        function funcao() {    
            document.getElementById("campo").value = document.getElementById("url").value;
        }
    </script>

  </body>
</html>

perform here: https://jsfiddle.net/eltonpering/3prg6pj0/

  • This way you are doing something you were already doing in mine, but yesterday I managed to find something in the Electron documentation. No main.js, at window startup, I put the following: mainWindow.webContents.executeJavaScript('document.getElementById("campo").value = "teste"'). But still, thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.