HTML JavaScript



A Script is a small program that is used with HTML to make web pages more attractive, dynamic, and interactive.

HTML <script> Tag

The HTML <script> tag is used to specify a client-side script. It may be an internal or external JavaScript which contains scripting statements, hence we can place <script> tag within <body> or <head>section.

It is mainly used to manipulate images, form validation and change content dynamically. JavaScript uses document.getElementById() method to select an HTML element.


Example :

<!DOCTYPE html>  
<html>  
 <body>  
  <h4>Use JavaScript to Change Text</h4>  
  <p id="test"></p>  
  <script>  
   document.getElementById("test").innerHTML =
    "Hello Friends";  
  </script>  
 </body>  
</html>

Output :


HTML events with JavaScript

An event is something which user does, or browser does such as mouse click or page loading are examples of events, and JavaScript comes in the role if we want something to happen on these events.

HTML provides event handler attributes which work with JavaScript code and can perform some action on an event.


Exmple : 

<!DOCTYPE html>
<html>
  <body>
    <h4>Click Event Example</h4>
    <p>Click on the button and you csn see a pop-up window with a message</p>
    <input type="button" value="Click" onclick="alert('Hi,how are you')" />
  </body>
</html>

Output :


More Exmple of HTML JavaScript :


document.getElementById("demo").style.fontSize = "30px";
document.getElementById("demo").style.color = "blue";
document.getElementById("demo").style.backgroundColor = "yellow";
document.getElementById("image").src = "picture.gif";