Learn Basic HTML Complete Course part 19

2 min read

HTML JavaScript

A Script is a small program which is used with HTML to make web pages more attractive, dynamic and interactive, such as an alert popup window on mouse click. Currently, the most popular scripting language is JavaScript used for websites.


Example

<!DOCTYPE html> 
<html> 
<body> 
<h4>JavaScript Date and Time example</h4> 
<button type="button" 
onclick="document.getElementById('demo').innerHTML = Date()"> 
Click me to display Date and Time.</button> 
<p id="demo"></p> 
</body> 
</html>    


Output is:

JavaScript Date and Time example click on below button to show output

  


JavaScript Date and Time example


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.

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

Output is:

Use JavaScript to Change Text

Hello Technoapps Development



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.

Example is:


JavaScript  click  button example to show output as below 

  

<!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>  

Click Event Example

Click on the button and you csn see a pop-up window with a message

HTML can have following events such as:


  • Form events : reset, submit, etc.
  • Select events : text field, text area, etc.
  • Focus event : focus, blur, etc.
  • Mouse events : select, mouseup, mousemove, mousedown, click, dblclick, etc.

Following are the list for Window event attributes:

Event NameHandler NameOccurs when
onBlurblurWhen form input loses focus
onClickclickWhen the user clicks on a form element or a link
onSubmitsubmitWhen user submits a form to the server.
onLoadloadWhen page loads in a browser.
onFocusfocusWhen user focuses on an input field.
onSelectselectWhen user selects the form input filed.


You may like these posts

  • HTML Formatting TagsHTML Formatting is a process of formatting text for better look and feel. HTML provides us ability to format text without using CSS. There are many formatt…
  • HTML Ordered List | HTML Numbered ListHTML Ordered List or Numbered List displays elements in numbered format. The HTML ol tag is used for ordered list. We can use ordered lis…
  • HTML LayoutsHTML layouts provide a way to arrange web pages in well-mannered, well-structured, and in responsive form or we can say that HTML layout specifies a way in which the we…
  • HTML ImageHTML img tag is used to display image on the web page. HTML img tag is an empty tag that contains attributes only, closing tags are not used in HTML image element.Let's s…
  • HTML ElementsAn HTML file is made of elements. These elements are responsible for creating web pages and define content in that webpage. An element in HTML usually consist of a sta…
  •  Introduction of HTMLHTML – HyperText Markup Language – The Language of Web Pages on the World Wide Web. 🔹HTML is a text formatting language.URL – Uniform Resource Locat…

Post a Comment