views:

80

answers:

3

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.

+1  A: 

You have to put a reference to the script.js file.

<script type="text/javascript" src="script.js"></script>

Cristian Boariu
The script.js is included in the document and I get the same error.
Emanuel
+1  A: 

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>
Langdon
The script.js is included in the document and I get the same error.
Emanuel
Your script.js file must not be in a sub-directory called "js" then, because the code works fine.
Langdon
+3  A: 

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.

tvanfosson