JavaScript

JavaScript

JavaScript

JavaScript is nothing but client side scripting language, which is developed by Brendan Eich in 1995.

Client-side scripting means program executed in the client web browser.

JavaScript not only make web page more attractive but also validate client input before submitting data to a server.

DOM in JavaScript

DOM stand for Document Object Model; it is a programming interface for web page

DOM traversing using JavaScript

  • World Wide Web Consortium HTML DOM standard tells us all HTML document(program) is a node.
  • Like its all element, text, other data etc.
  • We can categorized HTML document like,

·        According to above hierarchy HTML is root node, head and body is child node, and so on.

·        The HTML DOM slandered defines the html element as Object, properties, methods, events.

·        getElementById method and innerHTML property.

Example:

<html>

<body>

<p id="a"></p>

<script>

document.getElementById("a").innerHTML = "wel come";

</script>

</body>

</html>

OUTPUT

Welcome

·        getElementsByTag

Example:

<html>

<body>

<p id="a"> java script </p>

<script>

const e = document.getElementsByTagName("p");

document.getElementById("a").innerHTML = 'wel come' + e[0].innerHTML;

</script>

</body>

</html>

OUTPUT

Welcome  java script

·        getElementsByClass

Example:

 

<html>

<body>

<p id="a"> java script</p>

<p class="p"> java script </p>

<script>

const e = document.getElementsByClassName("p");

document.getElementById("a").innerHTML = 'wel come' + e[0].innerHTML;

</script>

</body>

</html>

OUTPUT

wel come java script

java script

Example:

<html>

<head>

<title>Addition </title>

</head>

<body>

<h1>Addition of Two Numbers</h1>

 

<label for="a">Enter the first number:</label>

<input type="number" id="a"><br><br>

 

<label for="b">Enter the second number:</label>

<input type="number" id="b"><br><br>

 

<button onclick="addNumbers()">Calculate</button>

<br><br>

<p id="result">The sum is:</p>

<script>

function addNumbers() {

  const a = parseFloat(document.getElementById("a").value);

  const b = parseFloat(document.getElementById("b").value);

 

    const c = a + b;

    document.getElementById("result").innerHTML = "The sum is: " + c;

  }

</script>

 

</body>

</html>

OUTPUT



Example:

<html>

<head>

<title>Addition</title>

</head>

<body>

<h1>Addition of Two Numbers</h1>

<script>

const num1 = parseInt(prompt('Enter the first number '));

const num2 = parseInt(prompt('Enter the second number '));

//add two numbers

const sum = num1 + num2;

window.alert("Addition is  "+sum);

// display the sum

</script>

</body>

</html>

Array

Array is nothing but collection of multiple values under single variable name.

syntax:
const arrayname=[item1,item2,.....itemn]

 We can declare an array in the following ways.

  • const  color=["red", "green", "blue"];
  • const color=[];
        color[0]="red";
        color[1]="green";
        color[2]="blue";
  • const color=new Array ["red", "green", "blue"]; 
Example:
<html>
<body>
<h1> Arrays</h1> 
<p>display an array elements individual:</p>

<p id="arr1"></p>

<p>The toString() method returns an array elements:</p>

<p id="arr2"></p>

<script>
const color = ["Red", "Orange", "green", "blue"];
document.getElementById("arr1").innerHTML = color[0]+','+color[1];
const fruits = ["Banana", "Orange", "Apple", "Mango"];
document.getElementById("arr2").innerHTML = fruits.toString();
</script>

</body>
</html>
output:






Comments

Popular posts from this blog

The structure of HTML and some basic tags

Frame tag and Example

Introduction and objective of HTML