View Full Version : Help customising this script

10-16-2008, 09:34 AM
Hey guys

This is so noob it isnt funny. But obviously I know absolutley nothing about JavaScript!

I was googling and found this code on the net. Did the job fine... but I need it so that onload, it shows 'this is paragraph 1' rather then nothing under the drop down box.

Also... I feel like there is junk on there that isn't needed to do something like this... If someone knows a better one I can use please tell me!

But the main issue is that it loads showing div id="one"... If you can show me a better way of doing this then that is a bonus!

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">


<meta http-equiv="content-type" content="text/html;charset=iso-8859-1">
<title>Untitled Page</title>
<script type="text/javascript"><!--

var lastDiv = "";
function showDiv(divName) {
// hide last div
if (lastDiv) {
document.getElementById(lastDiv).className = "hiddenDiv";
//if value of the box is not nothing and an object with that name exists, then change the class
if (divName && document.getElementById(divName)) {
document.getElementById(divName).className = "visibleDiv";
lastDiv = divName;

<style type="text/css" media="screen"><!--
.hiddenDiv {
display: none;
.visibleDiv {
display: block;
border: 1px grey solid;


<body bgcolor="#ffffff">
<form id="FormName" action="blah.php" method="get" name="FormName">
<select name="selectName" size="1" onchange="showDiv(this.value);">
<option selected value="one">first</option>
<option value="two">second</option>

<div id="one" class="hiddenDiv">This is paragraph 1.</div>
<div id="two" class="hiddenDiv">This is paragraph 2.</div>


Cheers for your time guys/girls :)

Philip M
10-16-2008, 10:22 AM
<body bgcolor="#ffffff" onload="showDiv('one')" >

"When I was a kid I used to pray every night for a new bike. Then I realised that The Lord doesn't work that way, so I stole one and asked him to forgive me."

10-16-2008, 11:33 AM
hm lol, I failed...

I tried putting it into the body onload but I left it as this.value ...

Shows I am an all time low for javascript basics!

Just curious - is it possible to do this without using the body tag?

For example, in the javascript code itself?

10-16-2008, 11:06 PM
Hm I'm sure this is easy to do though!

10-17-2008, 04:27 AM
Just curious - is it possible to do this without using the body tag?

For example, in the javascript code itself?


Though this is irrelevant to your problem, bgcolor is a deprecated attribute (http://www.codehelp.co.uk/html/deprecated.html).

Hope that helps.