0

My html:

<!doctype hmtl>
<html>
<head>
</head>
<body>
    <script type='text/javascript'>
        var playerId = 1;
        function event(action) {
            let req = new XMLHttpRequest('/act?id='+String(playerId)+'&action='+action);
            alert(action);
        }
    </script>
    <img src="up.png" width='100' height='100' onclick='event("up");'>
    <img src="down.png" width='100' height='100' onclick='event("down");'>
    <img src="left.png" width='100' height='100' onclick='event("left");'>
    <img src="right.png" width='100' height='100' onclick='event("right");'>
    <img src="fire.png" width='100' height='100' onclick='event("fire");'>
</body>
</html>

I know for sure the image is clickable. But why does the error message say event is not a function? I need help.

2
  • 3
    Just rename the function; event is not allowed. Commented May 10, 2017 at 0:50
  • Thank you! This is the answer. Commented May 10, 2017 at 0:52

1 Answer 1

3

This way, you are trying to access the global object window.event, so just rename your function.

Your own code, renaming the function event to test:

<!doctype hmtl>
<html>
<head>
</head>
<body>
    <script type='text/javascript'>
        var playerId = 1;
        function test(action) {
            let req = new XMLHttpRequest('/act?id='+String(playerId)+'&action='+action);
            alert(action);
        }
    </script>
    <img src="up.png" width='100' height='100' onclick='test("up");'>
    <img src="down.png" width='100' height='100' onclick='test("down");'>
    <img src="left.png" width='100' height='100' onclick='test("left");'>
    <img src="right.png" width='100' height='100' onclick='test("right");'>
    <img src="fire.png" width='100' height='100' onclick='test("fire");'>
</body>
</html>

Sign up to request clarification or add additional context in comments.

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.