How to Create a Js File
- Launch “Notepad.” Video of the Day.
- Type “myfile.js” and click the “Save” button. Now you have created a .js file. By adding,
write() The Document. write() method writes a string of text to a document stream opened by document.
- Open Notepad by pressing Window+R from your PC.
- Press ctrl+S to save the file in your system.
- After pressing ctrl+S it will ask for the name of your file.
- Give a name of the file with . JS extension i.e. “hello. js”.
You should put your JS code in a separate file because this makes it easier to test and develop. The question of how you serve the code is a different matter. Serving the HTML and the JS separately has the advantage that a client can cache the JS.
The getElementById Method
The most common way to access an HTML element is to use the id of the element. In the example above the getElementById method used id=”demo” to find the element.