Menus 111
Menus 111
Menus 111
4 Menus
Example:-
<head>
<title>Pull Down Menu</title>
<script>
function GetSelectedItem()
{
var ele=document.getElementById("select");
var str=ele.options[ele.selectedIndex].value;
alert("The Selected value is:" +str);
}
</script>
<body>
<select id="select">
<option value="c">C</option>
<option value="c++">C++</option>
<option value="java">JAVA</option>
<option value="javascript">JAVASCRIPT</option>
</select>
</body>
</html>
Output:-
function myfunction()
{
var select=document.getElementById("student");
var s=select.options.length;
for(i=s;i>=0;i--)
{
select.options.remove(i);
}
var x=document.getElementById("myselect").value;
if(x=="syco")
{
for(i=0;i<sy_subjects.length;i++)
{
opt=document.createElement("option");
opt.value=sy_subjects[i];
opt.textContent=sy_subjects[i];
select.appendChild(opt);
}
}
else if(x=="tyco")
{
for(i=0;i<ty_subjects.length;i++)
{
opt=document.createElement("option");
opt.value=ty_subjects[i];
opt.textContent=ty_subjects[i];
select.appendChild(opt);
}
}
else if(x=="class")
{
opt=document.createElement("option");
opt.value="class";
opt.textContent="Subjects";
select.appendChild(opt);
}
}
</script>
</head>
<body>
<select id="myselect" onchange="myfunction(this)">
<option value="class">Class</option>
<option value="syco">SYCO</option>
<option value="tyco">TYCO</option>
</select>
<select id="student" name="student">
<option value="0">Subjects</option>
</select>
</body>
</html>
Output:-
6.4.3 Validating Menu Selection
While filling information, user can forget to select an option in
menu. In such case, the incomplete information should not be
send to the server. To make fields compulsory to fill, we use
filed validation. We can validate menu selection by using
JavaScript. JavaScript is used for Client Side Validations. We
can check whether the input is correct or not. If input is
Incorrect then the error message can be generated and displayed
to the screen. By using JavaScript, the user gets prompt to fill
the proper information.
Example:-
<html>
<head>
<title>Validating Menu Selection</title>
<script>
function myfunction()
{
var select=document.getElementById("myselect");
var x=document.getElementById("myselect").value;
if(x =="Select Subjects")
{
alert("Plese Select subjects.");
}
else
{
document.getElementById("demo").innerHTML="You slected:"+x;
}
}
</script>
</head>
<body>
<select id="myselect">
<option value="Select Subjects">Select Subject</option>
<option value="oop">OOP</option>
<option value="Cgr">CGR</option>
<option value="dbms">DBMS</option>
<option value="dsu">DSU</option>
<option value="dte">DTE</option>
</select>
<br>
<input type="button" value="Submit" onclick="myfunction()">
<br>
<p id="demo"></p>
</body>
</html>
Output:-
6.4.4 Floating Menu
When user scrolls the webpage, sometimes we need a menu to
stick to the screen for particular operation. The menu which is
fixed while scrolling the webpage is known as floating menu. A
floating menu can be created by setting the style position of that
menu fixed. Following example shows the floating menu on
webpage.
Example:-
<html>
<head>
<title>Floating Menu</title>
<script>
function myfunction()
{
var select = document.getElementById("mySelect");
var x = document.getElementById("mySelect").value;
if(x == "Select Subject")
{
alert("Please select subject.");
}
else
{
document.getElementById("demo").innerHTML = "You selected:" + x;
}
}
</script>
</head>
<body>
<br> <br> <br>
<p>Floating menu. Scroll Down.</p>
<select id="mySelect" style="position:fixed">
<option value="Select Subject">Select Subject</option>
<option value="oop">OOP</option>
<option value="cgr">CG</option>
<option value="dbms">DBMS</option>
<option value="dsu">DSU</option>
<option value="dte">DTE</option>
</select>
<br><br>
<input type="button" value="Submit" onclick="myfunction()">
<br>
<p id="demo"></p>
<br> <br> <br> <br> <br> <br> <br> <br>
</body>
</html>
Output:-
6.4.5 Chain Select Menu
Chain select menu is a type of dynamic menu in which there
are more than one select menu present. The option select from
first menu decides the options of second menu and option
selected from second menu decides the option of the third
menu. For ex. There are three select menu are available first
for district second for tehsil of selected district and third for
village of selected tehsil. First we need to select district from
first select menu, then on the basic of selected district in first
menu the options of second menu decided i.e. tehsil of
selected district will be set as options to second menu. Then
we need to select the tehsil from second menu. Then the
villages of selected tehsil will be set to the third menu as
options. Then we can select the village.