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 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    generating html with javascript

    Hi!

    I have an external js file with this script. I'm not 100% sure it works but it should. Now I want to call the function displayopt in a html file in a form/select. How do I do that?

    Code:
    male = new Array()
    male[0]="Al"
    male[1]="Harry"
    male[2]="Paul"
    male[3]="Steven" 
    male[4]="Thomas"
    function displayopt(){
    for(i=0;i<male.length;i++){
    document.write("<option>"+male[i]);}}
    Code:
    <html>
    <head>
    <title>Untitled</title>
    <script src="my_js.js" 
      language="javascript" 
        type="text/javascript"></script>
    </head>
    <body>
    <form>
    <select>
    </select>
    </form>
    </body>
    Last edited by Franklin; 02-02-2005 at 10:57 PM.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Edinburgh, UK
    Posts
    402
    Thanks
    2
    Thanked 1 Time in 1 Post
    You're nearly there. What you said includes the js file, but doesn't call any of the funcitons. Just add <script language="JavaScript"> displayopt(); </script> wherever you want to call the function. Or add it to one of the onClick/onFocus/etc/etc... attributes and it should work fine
    Rich

    "An expert is a person who has made all the mistakes that can be made in a very narrow field."

  • #3
    New Coder
    Join Date
    Oct 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for your reply.

    I seem to be able to make one file. But when I try to extract the javascript to a js file I can't call the function anymore. Any idea??

    example
    Code:
    <html>
    <head>
    <title>Untitled</title>
    <script>
    male = new Array()
    male[0]="Al"
    male[1]="Harry"
    male[2]="Paul"
    male[3]="Steven" 
    male[4]="Thomas"
    function displayopt(){
    for(i=0;i<male.length;i++){
    document.write("<option>"+male[i]);}}
    </script>
    </head>
    <body>
    <form>
    <select>
    <script>displayopt();</script>
    </select>
    </form>
    </body>

  • #4
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    This works fine for me
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>51469</title>
    
    		<script type="text/javascript">
    			var male = new Array();
    			male[0]="Al";
    			male[1]="Harry";
    			male[2]="Paul";
    			male[3]="Steven";
    			male[4]="Thomas";
    
    			function displayopt()
    			{
    				for(var i = 0; i < male.length; i++)
    				{
    					document.write("<option>" + male[i] + "</option>");
    				}
    			}
    		</script>
    	</head>
    	<body>
    		<form>
    			<fieldset>
    				<select>
    					<script type="text/javascript">displayopt();</script>
    				</select>
    			</fieldset>
    		</form>
    	</body>
    </html>

  • #5
    New Coder
    Join Date
    Oct 2004
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It doesn't as two files; one html and one js, at least I can't make it work.

  • #6
    Senior Coder
    Join Date
    Jul 2004
    Location
    New Zealand
    Posts
    1,315
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Oh, I mis-read your post.

    html
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html lang="en">
    	<head>
    		<title>51469</title>
    
    		<script type="text/javascript" src="javascript.js"></script>
    	</head>
    	<body>
    		<form>
    			<fieldset>
    				<select>
    					<script type="text/javascript">displayopt();</script>
    				</select>
    			</fieldset>
    		</form>
    	</body>
    </html>
    javascript
    Code:
    var male = new Array();
    male[0]="Al";
    male[1]="Harry";
    male[2]="Paul";
    male[3]="Steven";
    male[4]="Thomas";
    
    function displayopt()
    {
    	for(var i = 0; i < male.length; i++)
    	{
    		document.write("<option>" + male[i] + "</option>");
    	}
    }


  •  

    Posting Permissions

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