OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

External javascript file not working in HTML file

  • Thread starter Thread starter Harsh Vardhan
  • Start date Start date
H

Harsh Vardhan

Guest
I'm trying to change the property of a div from a javascript file but it's not working, it's working if I put it inside body but I want to work it from external js file for some project.

HTML file

Code:
<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Calendar</title>
    <link rel="stylesheet" href="css/styles.css">
    <script src="javascript/cal.js"></script>
  </head>
  <body>

    <div id="container">

      <div id="calendar-view">

      </div>

      <div id="events-view">

      </div>

    </div>


  </body>
</html>

CSS file

Code:
*{
  box-sizing: border-box;
}

#calendar-view{
  float: left;
  width: 70%;
  background-color: blue;
  height: 200px;
}

#events-view{
  float: left;
  width: 30%;
  height: 200px;
  background-color: red;
}

#container::after{
  content: "";
  clear: both;
  display: table;
}

Javascript file

Code:
function startUp(){
  document.getElementById("calendar-view").style.display = "none";
}

startUp();

I'm trying to change the property of a div from a javascript file but it's not working, it's working if I put it inside body but I want to work it from external js file for some project.

<p>I'm trying to change the property of a div from a javascript file but it's not working, it's working if I put it inside body but I want to work it from external js file for some project.</p>
<p>HTML file</p>
<pre><code><!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Calendar</title>
<link rel="stylesheet" href="css/styles.css">
<script src="javascript/cal.js"></script>
</head>
<body>

<div id="container">

<div id="calendar-view">

</div>

<div id="events-view">

</div>

</div>


</body>
</html>
</code></pre>
<p>CSS file</p>
<pre><code>*{
box-sizing: border-box;
}

#calendar-view{
float: left;
width: 70%;
background-color: blue;
height: 200px;
}

#events-view{
float: left;
width: 30%;
height: 200px;
background-color: red;
}

#container::after{
content: "";
clear: both;
display: table;
}

</code></pre>
<p>Javascript file</p>
<pre><code>function startUp(){
document.getElementById("calendar-view").style.display = "none";
}

startUp();
</code></pre>
<p>I'm trying to change the property of a div from a javascript file but it's not working, it's working if I put it inside body but I want to work it from external js file for some project.</p>
 
Top