Learn Basic HTML Complete Course part 19

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.


Post a Comment

© Top Blog Pk. All rights reserved. Developed by Jago Desain