Java Script


JavaScript Tutorial


Admission Enquiry Form


Assignments of Javascript

Write a code to accept a number from the user and generate table of entered number using table tag. The table even odd rows color must be different.

<html>
<head>
<title>generate table</title>
<script>
function table()
{
var n,res=0,i,count=1;
n=parseInt(prompt("Enter any number"));
document.write("<table border='5' style='border-color:blue;'>");
for(i=1;i<=10;i++)
{
res=n*i;
if(count%2==0)
{
document.write("<tr style='color:red; font-size:24px;'><td> "+n+" </td><td> X </td><td> "+i+" </td><td> = </td><td> "+res+" </td></tr>");
}//end of if
else
{
document.write("<tr style='color:green; font-size:24px;'><td> "+n+" </td><td> X </td><td> "+i+" </td><td> = </td><td> "+res+" </td></tr>");
}//end of else
count++;
}//end of for loop
document.write("</table>");
}//end of function
</script>
</head>
<body>
<h2>Click on Button to Get the Required Number Table</h2>
<input type="button" value="Table" onClick="table()" style="font-size:18px;">
</body>
</html>



Write a code to ask the user, if the user wants to close the browser window or not.

<html>
<head>
<title>close browser window</title>
<script>
function check_window()
{
var x=confirm("Do you want to close window..");
if(x==true)
{
window.close();
}
}
</script>
</head>

<body>
<h2>Click the button to close the browser window</h2>
<input type="button" value="click" onClick="check_window()" style="font-size:18px;"/>
</body>
</html>



Write a code to apply innerHTML on multiple same tags.

<html>
<head>
<title>innerHTML using getElementsByTagName</title>
<script>
function tag_name()
{
var x,i,len;
x=document.getElementsByTagName("p");
len=x.length;
for(i=0;i<len;i++)
{
x[i].innerHTML="<font color='red'>compuhelp "+(i+1)+"</font>";
}
}
</script>
</head>

<body>
<p>This is para1</p>
<h3>This is heading3 tag</h3>
<p>This is para2</p>
<b>This is bold text</b>
<input type="button" value="click" onClick="tag_name()" style="font-size:18px;">
</body>
</html>






Write a code to select multiple courses using checkboxes.

<html>
<head>
<title>Use of CheckBox to select courses</title>
<script>
function select_course()
{
var x,i,len;
var str="";
x=document.getElementsByName("course");
len=x.length;
for(i=0;i<len;i++)
{
if(x[i].checked)
{
str+=x[i].value+"\n";
}
}//end of loop
alert("You have selected\n"+str);
}
</script>
</head>

<body>
<form>
<input type="checkbox" name="course" value="C" />C Programming Language <br>
<input type="checkbox" name="course" value="C++" />C++ Programming Language<br>
<input type="checkbox" name="course" value="Java" />Java Programming Language<br>
<input type="checkbox" name="course" value="Python" />Python Programming Language<br><br>
<input type="button" name="btn" value="course" onClick="select_course()">
</form>

</body>
</html>






Write a code to count the number of same elements in HTML page.

<html>
<head>
<title>Count how many same elements are there</title>
<script>
function count_elements()
{
var x=document.getElementsByTagName("p");
alert(x.length);
}
</script>
</head>

<body>
<p>Welcome to Compuhelp and Thanks to Visit us</p>
<p>Welcome to Compuhelp and Thanks to Visit us</p>
<p>Welcome to Compuhelp and Thanks to Visit us</p>
<p>Welcome to Compuhelp and Thanks to Visit us</p>
<p>Welcome to Compuhelp and Thanks to Visit us</p>
<input type="button" value="click" onclick="count_elements()" style="font-size:18px;"/>
</body>
</html>