Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

103
Views
Handle JS classes with import statements in html file?

I have 3 files.

  1. Connections.js
  2. Main.html.
  3. ScriptFile.js

I am copying only small part of the code here.

Connection.js has a class

import {net} from 'net';
export class connect {
Testfunction() {return "something"}
}

main.html has a function

<html> 
<head>
        <script src= 'Connections.js'></script>
        <script src= 'ScriptFile.js'></script> 
</head>
<body>
<p id="demo" onclick="myFunction()">Click me to change my text color.</p> 
</body>
</html>

ScriptFile.js

import { connect } from "./Connections.js";
const ObjConnect = new connect()
function myFunction() {
    document.getElementById("demo").innerHTML = ObjConnect.Testfunction();

  }

How do I make "text" in my html file to change to "something" when I click on it?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

First you have a typo at file name. It's Connection, not Connections in main.js

Case1. using module

  • main.html
<html>
  <head>
    <script type="module" src="Connection.js"></script>
    <script type="module" src="ScriptFile.js"></script>
  </head>
  <body>
    <p id="demo">Click me to change my text color.</p>
  </body>
</html>
  • ScriptFile.js
import { connect } from './Connection.js';

const ObjConnect = new connect();
function myFunction() {
  document.getElementById('demo').innerHTML = ObjConnect.Testfunction();
}

document.getElementById('demo').addEventListener('click', myFunction);

Case2. using script

  • main.html
<html>
  <head>
    <script src="Connection.js"></script>
    <script src="ScriptFile.js"></script>
  </head>
  <body>
    <p id="demo" onclick="myFunction()">Click me to change my text color.</p>
  </body>
</html>
  • Connection.js
class connect {
  Testfunction() {
    return 'something';
  }
}
  • ScripFile.js
const ObjConnect = new connect();
function myFunction() {
  document.getElementById('demo').innerHTML = ObjConnect.Testfunction();
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!