I love Free Software!

Pages

Funny Quotes to Think

When you say "I wrote a program that crashed Windows", people just stare at You blankly and say "Hey, I got those with the system, for free"

-Torvalds, Linus(1995-03-08)-

Saturday, February 6, 2010

Show or Hide html DIV based on dropdown menu selection

Java script in html will help you to show or hide div section in html based on selected value in drop down menu
You can add controls to each div section to show and hide controls based on drop down value. Below html file will explain you in deep

We have two javascripts. start() will start when you load html form and disp_div() will show and hide div section depending on your drop down value

<html>
<head>
<script language=JavaScript>
function start()
{
        var f=document.getElementById("first");
        var s=document.getElementById("second");
        var l=document.getElementById("last");
        f.style.display = 'none';
        s.style.display = 'none';
        l.style.display = 'none';
}
function disp_div() {
       var word = document.myform.distro.selectedIndex;
       var selected_text = document.myform.distro.options[word].text;
       var f=document.getElementById("first");
       var s=document.getElementById("second");
       var l=document.getElementById("last");
        if (selected_text == 'Debian'){
                 f.style.display = 'block';
                 s.style.display = 'none';
                 l.style.display = 'none';
        }else if (selected_text == 'Ubuntu'){
                f.style.display = 'none';
                s.style.display = 'block';
                l.style.display = 'none';
        }else if (selected_text == "Windows"){
                f.style.display = 'none';
                s.style.display = 'none';
                l.style.display = 'block';
        }
}     
</script>

<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
<title>which distro you like</title>
</head>
<body bgcolor="#BDEDFF" onload="start()"><br>
<FORM NAME="myform" >
<br><font size="3" face="Times">
Select Your favourate Operating System   
<select name="distro" onchange="disp_div()">
<option value="debian">Debian</option>
<option value="ubuntu">Ubuntu</option>
<option value="win">Windows</option>
</select>
<br>
<div id="first">
<br>Avoid the Gates of Hell. Use Linux <br></div>
<div id="second">
<br>Ubuntu Linux IS user friendly... Linux for human beings <br></div>
<div id="last">
<br>Microsoft sells you Windows... Linux gives you the whole house <br></div>
</form>
</body>
</html>