---
title: "How to insert JavaScript code in HTML code?"  
description: "How to insert JavaScript code in HTML code?"  
author: "Ravi Vishwakarma"  
published: 2021-07-19  
canonical: https://answers.mindstick.com/qa/93726/how-to-insert-javascript-code-in-html-code  
category: "web application"  
tags: ["javascript", "web application development", "web development"]  
reading_time: 3 minutes  

---

# How to insert JavaScript code in HTML code?

how to [insert](https://www.mindstick.com/articles/81/insert-update-delete-records-in-csharp-dot-net) [javascript](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [code](https://www.mindstick.com/articles/331918/why-is-code-optimization-important) in [html](https://www.mindstick.com/articles/12368/why-are-html-themes-the-first-selection-of-designers) page.

## Answers

### Answer by Ethan Karla

In HTML, JavaScript code is inserted between script tag (<script> and </script>). We can placed any number of JavaScript file in HTML. Script can be placed in the <body> and <head> tags of a HTML page. JavaScript code is written in two way. 1. Internal 2. External **Internal way** In this way write the script code in script tag but script tag placed in head or body tags. If we placed script code in head tag then first of all load the JS file then load the body tag data and if placed in body tag then load the body tag, after loading the body tag then load the script code.

```
 <script>
  //variable
  //function
  //objects
  //class
  //properties
  //message box
 </script>
```

**Example:**

```
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title>JS Demo</title>
</head>
<body>
    <p>JS demo</p>
    <p id='id1'></p>
    <script>
        document.getElementById('id1').innerHTML = 'this is body tag occur';
    </script>
</body>
</html>
```

**External way** In this way write the script code in a .js file (JavaScript Extension) but doesn’t use the script tag.

```
 First-js-file.js (creating this file)
  //variable
  //function
  //objects
  //class
  //properties
  //message box
```

**Example:**

```
JavaScript.js
function changeColor() {
    var name = window.document.getElementById('color');
    alert('Your selected color is : ' + name.value);
    name.style.backgroundColor = name.value;
    window.document.body.style.backgroundColor = name.value;
}
```

```
htmlDemo.html
<!DOCTYPE html>
<html lang='en' xmlns='http://www.w3.org/1999/xhtml'>
<head>
    <meta charset='utf-8' />
    <title>JS Demo</title>
    <style type='text/css'>
        * {
            margin:10px;
            padding:0px;
        }
        select {
            width:100px;
        }
    </style>
    <script src='JavaScript.js' type='text/javascript'></script>
</head>
<body>
    <span>Select Color name  : </span>
    <select name='color' id='color' >
        <option value='red'>Red</option>
        <option value='yellow'>Yellow</option>
        <option value='white'>White</option>
        <option value='pink'>Pink</option>
        <option value='green'>Green</option>
    </select>
    <button type='button' onclick='changeColor()' value=' Submit '> Submit </button>
</body>
</html>
```

**Output :** ![How to insert JavaScript code in HTML code?](https://answers.mindstick.com/questionanswer/5a72aabd-dd41-435a-b267-8094fb1604fe/images/7c21d04e-8ecb-4c69-a40a-1143bbe4915a.png) **\**


---

Original Source: https://answers.mindstick.com/qa/93726/how-to-insert-javascript-code-in-html-code

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
