Javascript is not communicating with html

Asked

Viewed 62 times

2

So I’m making a pc pliative with the electron who uses html, js e css but when I was trying to make a value in html change with my js it did nothing but when I put the script into html it worked, problem is that I needed to have a js file just to make these changes!.

My HTML

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>EComerce App Slime</title>
    <link rel="stylesheet" href="css/index.css">
    <script src="js/index.js"></script>
  </head>
  <body>
    <div id="UpperBar">
      <p class="MASlime">MASlime</p>
      <p class="Home">HOME</p>
    </div>
    <div id="HomePage">
      <div id="Totals">
        <p class="TotalEarnings">Total Earnings</p>
        <p class="TotalOrders">Total Orders</p>
        <p id="Earnings">HIII</p>
      </div>
      <div id="NewOrder">

      </div>
      <div id="Recipe">
        <div id="Small">

        </div>
        <div id="Medium">

        </div>
        <div id="Big">

        </div>
      </div>
    </div>
  </body>
</html>

MY JS

var element = document.getElementById("Earnings");
element.innerHTML = "100€";

1 answer

1


You are inserting your <script> within the <head>. Try to declare your <script> before the end of the tag <body> as below:

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>EComerce App Slime</title>
    <link rel="stylesheet" href="css/index.css">
  </head>
  <body>
    <div id="UpperBar">
      <p class="MASlime">MASlime</p>
      <p class="Home">HOME</p>
    </div>
    <div id="HomePage">
      <div id="Totals">
        <p class="TotalEarnings">Total Earnings</p>
        <p class="TotalOrders">Total Orders</p>
        <p id="Earnings">HIII</p>
      </div>
    </div>

    <!-- 
     ####################
     OS ARQUIVOS DE SCRIPTS VEM AQUI 
     ####################
    -->
    <script src="js/index.js"></script>
  </body>
</html>

And then insert your function JS in the separate file index.js

I hope I helped. If solved, contribute by marking as correct answer. If it does not work, detail some more information options, for example: which shows the browser developer console

Success bro!

  • 1

    Thanks, it worked great! After a lot of research the only thing that needed to come to stackoverflow!

Browser other questions tagged

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