2

I want, my HTML document to output random numbers for Dungeons and Dragons. However, the code isn't working and I can't workout why.

<!DOCTYPE html>
<html>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js">
$(document).ready(function(){
  $("#d4").click(function(){
    var out = Math.random(1,4);
    alert(out);
  };
});
</script>
</head>
<body>
  <p id ="d4">Roll a d4</p>
</body>
</html>
2
  • How isn't it working? Commented Feb 23, 2014 at 1:04
  • 1
    put your document.ready code in separate script tag after jquery script tag. Commented Feb 23, 2014 at 1:06

3 Answers 3

12

The script tag cannot have the src attribute and a body, you need to use separate script tags

<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script>
$(document).ready(function () {
    $("#d4").click(function () {
        var out = Math.random(1, 4);
        alert(out);
    }); //missing ) here
});
</script>

Script src

This attribute specifies the URI of an external script; this can be used as an alternative to embedding a script directly within a document. script elements with an src attribute specified should not have a script embedded within its tags.

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

Comments

2

After alert you need to close the parenthesis to the click function.

$(document).ready(function(){
    $("#d4").click(function(){
        var out = Math.random(1,4);
        alert(out);
    }); // <-- here
});

Comments

0

Missing click function's closing parenthesis.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.