...

View Full Version : generating html with javascript



Franklin
02-02-2005, 10:08 AM
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?



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]);}}




<html>
<head>
<title>Untitled</title>
<script src="my_js.js"
language="javascript"
type="text/javascript"></script>
</head>
<body>
<form>
<select>
</select>
</form>
</body>

Badman3k
02-02-2005, 02:44 PM
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 :thumbsup:

Franklin
02-02-2005, 11:20 PM
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


<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>

hemebond
02-02-2005, 11:31 PM
This works fine for me
<!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>

Franklin
02-03-2005, 01:00 AM
It doesn't as two files; one html and one js, at least I can't make it work. :)

hemebond
02-03-2005, 03:14 AM
Oh, I mis-read your post.

html
<!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
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>");
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum