Js Lab
Js Lab
Js Lab
<html>
<head>
<title>Display Alert Message on Button Click Event.</title>
<script type="text/javascript">
function showMessage(){
alert("Hello friends, this is alert message.");
}
</script>
</head>
<body>
<h1>Display Alert Message on Button Click Event.</h1>
<input type="button" id="btnShowMsg" value="Click Me!" onClick='showMessage()'/>
</body>
</html>
OutPut
alert(`You are ${age} years old!`); // You are 100 years old!
</script>
JS LAb
3. Click button to get innerHtml example
<!DOCTYPE html>
<html>
<body>
<h1>My Web Page</h1>
<p id="myP">This is a p element.</p>
<div id="myDIV">This is a div element.</div>
<script>
document.getElementById("myP").innerHTML = "Hello BCA Notes Nepal .";
document.getElementById("myDIV").innerHTML = "I need NOtes?";
</script>
</body>
</html>
Output
JS LAb
OutPut
JS LAb
Function
function switchCase() {
var text;
var fruits = document.getElementById("userInput").value;
switch(fruits) {
case "1":
text = "Today is Sunday";
break;
case "2":
text = "Today is Monday";
break;
case "3":
text = "Today is Tuesday";
break;
case "4":
text = "Today is Wednesday";
break;
case "5":
text = "Today is Thursday";
break;
case "6":
text = "Today is Friday";
break;
case "7":
text = "Today is Saturday";
break;
default:
text = "Invalid Input";
}
document.getElementById("switchCaseID").innerHTML = text;
}
OutPut
JS LAb
Function
function addNumbers() {
var x = 15;
var y = x + 1;
document.getElementById("addNumbersID").innerHTML = y;
}
OutPut
JS LAb
OutPut
Code
<button onclick="createInterval()">Try it</button>
Function
function createInterval() {
}
OutPut
<p id="printSumID"></p>
Function
function printSum() {
var total = 0;
for(var i = 1; i <= 10; i++){
total += i;
}
document.getElementById("printSumID").innerHTML = total;
}
Output
JS LAb
13 . Form validation
Code
</form>
</form>
Function
function Formvalidate(){
var name=document.myform.name.value;
var password=document.myform.password.value;
if (name==null || name==""){
return false;
}else if(password.length<6){
return false;
JS LAb
}
function validateEmail()
var x=document.myform2.email.value;
var atposition=x.indexOf("@");
var dotposition=x.lastIndexOf(".");
return false;
}
OutPut
<p id="p01"></p>
Funtion
function tryCatch() {
JS LAb
var message, x;
message = document.getElementById("p01");
message.innerHTML = "";
x = document.getElementById("tryCatchID").value;
try {
x = Number(x);
catch(err) {
}
OutPut
JS LAb
console.log('You clicked yes.');
else {
}
OutPut
}
Output
Code:
<html>
JS LAb
<body>
<script>
var numbers = [65, 44, 12, 4];
numbers.forEach(myFunction)
function myFunction(item, index, arr) {
arr[index] = item + 10;
document.write(arr[index] + " ");
}
</script>
</body>
</html>
OutPut
JS LAb