0

I'm encountering an error that doesn't seem to make sense. Chrome's console is saying Uncaught ReferenceError: clicked_server is not defined

I tried almost everything to fix it but the error itself doesn't make much sense

<script>
    var selected_char = 'X';

    function draw_list () {
        // lets draw an x in all server in the array
        var server_id = 0;
        var draw_servers = new Array();
        draw_servers = document.getElementById("server_arr").value.split(";");

        foreach(draw_servers as server_id) {
            document.getElementById("server("+server_id+")").innerHTML = selected_char;
        }    
        // Update the counter and servers array
        server_count_field.innerHTML = "Buy Server("+servers_array.length+")";
    }

    function clicked_server(server_id) {
         var clicked = document.getElementById("server("+server_id+")").innerHTML;
         if (clicked == selected_char) remove_server(server_id);
         else add_server(server_id);
    }

    window.onload = draw_list();


    function add_server(server_id) {
        // select a server for purchase
        var servers_array = document.getElementById("server_arr").value;

        if(servers_array.length > 0) servers_array = servers_array + ";" + server;
        else servers_array = server;

        document.getElementById("server_arr").value = servers_array;
    }
</script> 

My HTML is working perfectly. Here it is

<form action="dobuyserver.php" method="POST">
    <input type="hidden" name="server" id="server_arr"/>
    <input type="submit" value="Buy Server(0)" id="server_count"/>
    </form>

<td style="background-color:##000000;" onclick="clicked_server(9)"><font color="#FFFFFF">
                        <strong>
                        <span id="server(9)">
                        &nbsp;
                        </span>
                        </strong>
                        </font></td>
10
  • Try putting the javascript before the HTML in your page. Commented Jan 19, 2014 at 21:26
  • The javascript is before the HTML Commented Jan 19, 2014 at 21:26
  • 1
    Make sure the JavaScript is actually included in the page, too. Commented Jan 19, 2014 at 21:26
  • Better to remove inline JS and have it separated... anyway do you have a live link? Commented Jan 19, 2014 at 21:27
  • It is actually included as well. I see it perfectly using Chrome's inspect element. Commented Jan 19, 2014 at 21:27

4 Answers 4

4

As per my understanding you need to use

 window.onload = draw_list;  

instead of

 window.onload = draw_list();
Sign up to request clarification or add additional context in comments.

Comments

1

The reason for the stated error is that your javascript fails before getting to your function.

foreach(draw_servers as server_id) {

This line is invalid, so the js blows up and never goes beyond that line.

1 Comment

Anyone care to comment on the downvote? This is the actual answer to the question asked. The other answers point out an error but not the error that causes the Uncaught ReferenceError.
0

Replace window.onload = draw_list(); with window.onload = draw_list;

Comments

0

window.onload

Use window.onload = draw_list; instead of window.onload = draw_list();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.