0

Excuse me if I say something stupid here, I'm a bit of a noob at jQuery. I was working on a button in HTML/jQuery that is supposed to trigger a prompt when it was clicked, and then return another number. The problem is that the prompt doesn't trigger when I click on the button.

JavaScript:

$(document).ready(function() {
    $('#Progress-Button').click(function() {
        var question = prompt("Enter number of hours worked:", "Enter here");
        if (isNaN(Number(question)) === false) {
            var width = question * 10;
            return width;
        } else {
          question = prompt("That is not a number; Enter the number of hours worked.", "Enter here");
        }
    )
})

HTML:

<html>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="index.js"></script>
<link rel="stylesheet" href="index.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">    </script>
</html>
<head>
<title>Ian TEST</title>
</head>
<body>
<div id="main">
    <div id="Progress-BG"></div>
    <div id="Progress-Bar"></div>
    <button id="Progress-Button">Click Me</button>
</div> 
</body>
Ian
  • 5
  • 4

4 Answers4

1

Your code is running smoothly, the only problem is you did not properly closed after the else

});

Try your code below

$(document).ready(function() {
  $('#Progress-Button').click(function() {
    var question = prompt("Enter number of hours worked:", "Enter here");
    if (isNaN(Number(question)) === false) {
      var width = question * 10;
      return width;
    } else {
      question = prompt("That is not a number; Enter the number of hours worked.", "Enter here");
    }
  });
})
Fil
  • 8,225
  • 14
  • 59
  • 85
0

Your else block is missing a curly brace, and some semicolons are missing as well. Try this:

$(document).ready(function() {
  $('#Progress-Button').click(function() {
    var question = prompt("Enter number of hours worked:", "Enter here");
    if (isNaN(Number(question)) === false) {
      var width = question * 10;
      alert(width);
      return width;
    } else {
      question = prompt("That is not a number; Enter the number of hours worked.", "Enter here");
    }
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="main">
    <div id="Progress-BG"></div>
    <div id="Progress-Bar"></div>
    <button id="Progress-Button">Click Me</button>
</div>
MJH
  • 2,301
  • 7
  • 18
  • 20
0

Looks like you have some errors in your HTML, which are likely contributing to this. The <script>, <meta>, and <link> elements should be inside of <head>. Both <head> and <body> should be within <html>:

<html>
    <head>
        <title>Ian TEST</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="index.js"></script>
        <link rel="stylesheet" href="index.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js">    </script>
    </head>
    <body>
        <div id="main">
            <div id="Progress-BG"></div>
            <div id="Progress-Bar"></div>
            <button id="Progress-Button">Click Me</button>
        </div> 
    </body>
</html>
nbrooks
  • 18,126
  • 5
  • 54
  • 66
  • For some more details on the structure of an HTML document, have a look at this [W3 specification](https://www.w3.org/TR/html401/struct/global.html#h-7.1). – nbrooks Sep 26 '16 at 01:51
-4

You should use:

jQuery(document).ready(function($) {
   enter code here

Not this

$(document).ready(function() {
enter code here

...

Quy Truong
  • 413
  • 3
  • 9