Click here to Skip to main content
15,886,574 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i'm making a text adventure to try to learn javascript and im having problems with displaying integer values to my page. for example in the bellow code i want to display the users HP when you type attack. but nothing shows on the page. what am i doing wrong?











<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="scripts/jquery.js" type="text/javascript"></script>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>The Game!!</title>
</head>

<body>
<script>
var userHP = 500;

var hits = Math.floor(Math.random() * 100) - mobHP;

$(document).ready(function() {
$("#compass").fadeIn(3000);
$("#message_begin").fadeIn(3000);
$("#area_main").fadeIn(3000);
$("#command_line").fadeIn(3000);
$("form").submit(function() {
var input = $("#command_line").val();


//
//help
//
if (input.indexOf("help") > -1) {
if (input == "help") {
$("#message_help").clone().insertBefore("#placeholder").fadeIn(1000);
}
}
//
//end help
//
else if (input.indexOf("enter") > -1) {
if (input == "enter") {
$("#message_enter").clone().insertBefore("#placeholder").fadeIn(1000);
}
}
//
//end help
//
else if (input.indexOf("attack") > -1) {
if (input == "attack"){
$("#message_attack").clone().insertBefore("#placeholder").fadeIn(1000);

}}
</script>




<p id="message_attack"><script>userHP</script>you attack</p>






<form >
<input type="text" id="command_line" size="50" autofocus="autofocus" />
</form>




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

</body>

</html>
Posted
Updated 18-May-15 4:58am
v3

1 solution

sorry just an error in me copying it across, it still doesn't display a value.

someone on another forum said the following:

Quote:



$(userHP - hits).insertBefore("#placeholder").fadeIn(1000);


That line is passing a number (userHP minus hits) to jQuery. I don't think jQuery does anything when you pass it a number. You would have to create a new element (a span, a div, etc) with the text that you want in it, and then you can pass that element to jQuery.


no really sure what he means by this though
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900