Page 1 of 1

Javascript code working in html but not in JS file.

Posted: Tue Feb 06, 2007 10:48 pm
by tecktalkcm0391
I have this:

Code: Select all

function make(){
	
myPara = document.getElementById("paraID");

 
form = document.createElement("form");
form.setAttribute("method","post");
form.setAttribute("action","");
form.setAttribute("name","submit_form");
form.setAttribute("id","submit_form");


first_name = document.createElement("input");
first_name.setAttribute("type","text");
first_name.setAttribute("name","first_name");
first_name.setAttribute("value","chris");
form.appendChild(first_name);
 
myPara.appendChild(form);

} 
With this html:

Code: Select all

<body onload="make();"><div id="paraID"></div>
</body>
How come if I put the code in the header, it works, and if I put it in a seperate JS file I get an error of "Object Expected"?

Posted: Tue Feb 06, 2007 11:02 pm
by superdezign
Sounds like a document error. Try document.form (and maybe change that variable name) and document.first_name.

Posted: Wed Feb 07, 2007 12:17 am
by nickvd
When you put the js into an external file, did you remove the onload="make();" attribute?

Posted: Wed Feb 07, 2007 12:33 am
by Kieran Huggins
I'm willing to bet it's an inclusion problem. Your script tag MUST have a closing tag (not a single, closed element) and must specify src="/path/to/your.js"

so make it look like this:

Code: Select all

<script type="text/javascript" src="/path/to/your.js"></script>
The "type" attribute is optional, but I use it anyway.

Make sure the element is empty as well.

Posted: Wed Feb 07, 2007 3:44 pm
by tecktalkcm0391
My <script></script> is fine, and everything else is fine, but I ran it in Firefox to get the error, and it says make(); is not defined, but its in the external js file. Any ideas why?

Posted: Wed Feb 07, 2007 4:17 pm
by Kieran Huggins
is your path correct? Recheck your absolute/relative directories and your spelling.

Posted: Wed Feb 07, 2007 4:39 pm
by tecktalkcm0391
i got it to now just give me myPara has no properties... any ideas? [it was a */ causin the error before]