jQuery code doesn't work for some mysterious reasons

I have been trying to find what is wrong with this super-simple code for a long time now, but just can’t find it. Please help I’m desperate.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(function () {
    $("#mybutton").click(function {
        var color = "#" + Math.floor(Math.random() * 16777215).toString(16);
        $("body").css("background-color", color);
    });
});
</script>
<button id="mybutton">Change background color!</button>

have a look in the error console (F12), it’s listed there.

2 Likes

Its a basic syntax error. You are missing parentheses in this line:
$("#mybutton").click(function {

It should be:
$("#mybutton").click(function() {

1 Like
 $("#mybutton").click(function() {                         
        var color = "#" + Math.floor(Math.random() * 16777215).toString(16);
        $("body").css("background-color", color);
    });

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.