In this article, we will discuss how we
create a simple menu in javascript, for this follow these steps:
Step1: First we create two tables like
this:
Step 2: Now we write the javascript function for the onmouseover event:
- <script language="javascript">
- function Show1() {
- document.getElementById("tdmain1").style.backgroundColor = "#780000";
- document.getElementById("tdmain1").style.color = "White";
- document.getElementById("tr1").style.display = 'block';
- document.getElementById("tr2").style.display = 'none';
- document.getElementById("tr3").style.display = 'none';
- document.getElementById("tr4").style.display = 'none';
- document.getElementById("tr5").style.display = 'none';
- document.getElementById("tr6").style.display = 'none';
- }
-
- function Show2() {
- document.getElementById("tdmain2").style.backgroundColor = "#780000";
- document.getElementById("tdmain2").style.color = "White";
- document.getElementById("tr2").style.display = 'block';
- document.getElementById("tr1").style.display = 'none';
- document.getElementById("tr3").style.display = 'none';
- document.getElementById("tr4").style.display = 'none';
- document.getElementById("tr5").style.display = 'none';
- document.getElementById("tr6").style.display = 'none';
-
- }
-
- function Show3() {
- document.getElementById("tdmain3").style.backgroundColor = "#780000";
- document.getElementById("tdmain3").style.color = "White";
- document.getElementById("tr3").style.display = 'block';
- document.getElementById("tr2").style.display = 'none';
- document.getElementById("tr1").style.display = 'none';
- document.getElementById("tr4").style.display = 'none';
- document.getElementById("tr5").style.display = 'none';
- document.getElementById("tr6").style.display = 'none';
-
- }
-
- function Show4() {
- document.getElementById("tdmain4").style.backgroundColor = "#780000";
- document.getElementById("tdmain4").style.color = "White";
- document.getElementById("tr4").style.display = 'block';
- document.getElementById("tr2").style.display = 'none';
- document.getElementById("tr3").style.display = 'none';
- document.getElementById("tr1").style.display = 'none';
- document.getElementById("tr5").style.display = 'none';
- document.getElementById("tr6").style.display = 'none';
-
- }
-
- function Show5() {
- document.getElementById("tdmain5").style.backgroundColor = "#780000";
- document.getElementById("tdmain5").style.color = "White";
- document.getElementById("tr5").style.display = 'block';
- document.getElementById("tr2").style.display = 'none';
- document.getElementById("tr3").style.display = 'none';
- document.getElementById("tr4").style.display = 'none';
- document.getElementById("tr1").style.display = 'none';
- document.getElementById("tr6").style.display = 'none';
-
- }
-
- function Show6() {
- document.getElementById("tdmain6").style.backgroundColor = "#780000";
- document.getElementById("tdmain6").style.color = "White";
- document.getElementById("tr6").style.display = 'block';
- document.getElementById("tr2").style.display = 'none';
- document.getElementById("tr3").style.display = 'none';
- document.getElementById("tr4").style.display = 'none';
- document.getElementById("tr5").style.display = 'none';
- document.getElementById("tr1").style.display = 'none';
-
- }
- </script>
Here we set the property according to our requirement.
The Output Will be:
Step3: Now we write the function for the
onmouseout event in the javascript:
- function Hide1() {
- document.getElementById("tdmain1").style.backgroundColor = "Gray";
- document.getElementById("tdmain1").style.color = "Black";
- }
-
- function Hide2() {
- document.getElementById("tdmain2").style.backgroundColor = "Gray";
- document.getElementById("tdmain2").style.color = "Black";
- }
-
- function Hide3() {
- document.getElementById("tdmain3").style.backgroundColor = "Gray";
- document.getElementById("tdmain3").style.color = "Black";
- }
-
- function Hide4() {
- document.getElementById("tdmain4").style.backgroundColor = "Gray";
- document.getElementById("tdmain4").style.color = "Black";
- }
-
- function Hide5() {
- document.getElementById("tdmain5").style.backgroundColor = "Gray";
- document.getElementById("tdmain5").style.color = "Black";
- }
-
- function Hide6() {
- document.getElementById("tdmain6").style.backgroundColor = "Gray";
- document.getElementById("tdmain6").style.color = "Black";
- }