Consider:
File script.js,
function AdaugaComboBox(id, name){
var select_tag = document.getElementById(id);
select_tag.innerHTML += "<select name='"+name+"'><option value='0'>Selecteaza autor</option></select><br/>";
return true;
}
and file index.html:
<html>
<head>
<script src="js/script.js" type="text/javascript"></script>
</head>
<body>
<table>
<tr>
<td id="autori">...</td>
</tr>
<tr>
<td>
<input type="button"
value="Adauga autor"
onclick="AdaugaComboBox('autori', 'autori[]')"/>
</td>
</tr>
</table>
</body>
</html>
The scope of the function is to add a combo box to the specific TD in the TABLE. But when I press the button this error appears:
AdaugaComboBox is not defined
Why?
Update:
!!! I've fixed it. The problem was with another function.
-
2script.js is not included anywhere in your HTML...Langdon– Langdon2010年01月27日 12:32:18 +00:00Commented Jan 27, 2010 at 12:32
-
Where in the document tree is the function defined?Jacob Relkin– Jacob Relkin2010年01月27日 12:32:49 +00:00Commented Jan 27, 2010 at 12:32
-
The script.js is included in the document and I get the same error.Emanuel– Emanuel2010年01月27日 12:40:21 +00:00Commented Jan 27, 2010 at 12:40
-
1The code that you have posted works fine for me in Chrome and IE - no errors. If this is a shortened sample of your code, the error must be somewhere else.Andy E– Andy E2010年01月27日 12:46:37 +00:00Commented Jan 27, 2010 at 12:46
-
I bet your script.js file isn't in a "js" sub directory, because it works fine.Langdon– Langdon2010年01月27日 12:50:49 +00:00Commented Jan 27, 2010 at 12:50
3 Answers 3
If the script is included in your HTML, then it's possible that you don't have the path correct based on the location of the HTML file. Check with Firefox/Firebug to make sure that the JS file is being downloaded correctly.
Comments
Your HTML should be:
<html>
<head>
<script src="script.js" type="text/javascript"></script>
</head>
<body>
<table>
<tr>
<td id="autori">...</td>
</tr>
<tr>
<td>
<input type="button" value="Adauga autor" onclick="AdaugaComboBox('autori', 'autori[]')"/>
</td>
</tr>
</table>
</body>
</html>
You have to put a reference to the script.js file.
<script type="text/javascript" src="script.js"></script>