JQuery selector not working

ive was trying to figure out why my JavaScript code was not working. The warning works fine, but when I try to write in a div, it does nothing. What am I doing wrong? I tried to answer Google, but it is not too useful.

 <!DOCTYPE html> 

<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<script src="gamejavascript.js" type="text/javascript"> </script>  

<link rel="stylesheet" type="text/css" href="gameStyle.css">

<title>Text Adventure RPG</title>   

</head>

<body>
<p>hey  </p>
    <div id="eventWindow">
        <div id="title">Black Forest</div>
        <div id="eventContent">You have entered the black forest. Lucky you!</div>
    </div>
    <div id="itemList">Hey hey hey </div> 

    <div id="userStat"> </div> 

</body>

</html>

My javascript

$("p").append("HEY HEYH YEYEHE."); 
alert("You madam are a horse!");
$("#userStat").html("Hey baby"); 



/* var user = function(attack,health)
{
this.attack = attack; 
this.health = health;
}; 

var player = new user(10,100); */ 

Thank you so much for your help!

-Brent

+4
source share
3 answers

You need to place your JavaScript at the end of the page before the closing body tag or in the head and wrap it in readiness for the document , Example:

$( document ).ready(function() {
    // your code here
});

JsFiddle example (placing code at the end of a document)

, , .

+8

, jquery

$(document).ready(function() {
   $("p").append("HEY HEYH YEYEHE.");
});
+5

To run jQuery correctly, you first need to load the html content. For this reason, you should put your code in a ready-made document handler. Try the following:

$( document ).ready(function() {
    // put your code here as it is 
});
+1
source

Source: https://habr.com/ru/post/1524883/


All Articles