Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help customising this script

    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!

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
    
    <html>
    
    	<head>
    		<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;
    	}
    	
    	document.getElementById(divName).style.display='';
    }
    //-->
    </script>
    		<style type="text/css" media="screen"><!--
    .hiddenDiv {
    	display: none;
    	}
    .visibleDiv {
    	display: block;
    	border: 1px grey solid;
    	}
    
    --></style>
    	</head>
    
    	<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>
    			</select>
    		</form>
    		
    		<div id="one" class="hiddenDiv">This is paragraph 1.</div>
    		<div id="two" class="hiddenDiv">This is paragraph 2.</div>		
    	</body>
    
    </html>
    Cheers for your time guys/girls
    Last edited by wilorichie; 10-16-2008 at 09:19 AM.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    <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."

  • #3
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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?

  • #4
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hm I'm sure this is easy to do though!

  • #5
    Senior Coder rangana's Avatar
    Join Date
    Feb 2008
    Location
    Cebu City, Philippines
    Posts
    1,752
    Thanks
    65
    Thanked 372 Times in 365 Posts
    Quote Originally Posted by wilorichie View Post
    Just curious - is it possible to do this without using the body tag?

    For example, in the javascript code itself?
    Code:
    window.onload=showDiv('one');
    Though this is irrelevant to your problem, bgcolor is a deprecated attribute.

    Hope that helps.
    Learn how to javascript at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •