-1

I am trying to make a life simulator (web-based) with JavaScript and jQuery and when I took the code and ran it, the console said ReferenceError: '$' is not defined at /code.js 10:1 The jQuery $ worked before I added this: $('#name').html(name); I don't know what the problem is. This is my code:

var name = prompt('What is your name?');
var money = 1000;
var hunger = 100;
var hygiene = 100;
var sleep = 100;
var favColor = prompt('What is your favorite color?\nIf there is 2 words, combine them.\nDo not capitalize the color.', 'green');

var start = true;

$('#name').html(name);
$('#color').css('color', favColor);

function bed() {
    $('#bed').html('You are sleeping');
    var sleeping = setInterval(function() {
        if (sleep == 100) {
            this.clearInterval();
            $('#bed').html('You are awake');
        } else {
            sleep++;
        }
    }, 500);
}

1 Answers1

3

Add jQuery in header section in the code.

  • Ok, but when I do that, the jQuery code doesn't run, it doesn't give me an error in the console, and it doesn't run so it changes the `innerHTML` of the element with the `id` of 'name'. @David – BraylanBB121 Apr 16 '20 at 18:00
  • 1
    @BraylanBB121: If you can provide an example demonstrating the problem in a question, we can likely help with that. But "the jQuery code doesn't even run" isn't a particularly clear description of a problem. – David Apr 16 '20 at 18:01
  • Use document.ready event first, and then put the code in to that event, it means first the page will be properly updated with html and then the jQuery code will execute. –  Apr 16 '20 at 18:01