/    Sign up×
Community /Pin to ProfileBookmark

Need help reconciling Integers and HTML elements

Alright, before i start, i’m very new to javascript, i started teaching myself a few days ago (and took a break from it for 2 of those days). Anyway, i’m having some trouble with a pretty simple problem.

I want to have multiple variables, all connected to a ‘pool’. You may add or subtract from each one, which would subtract or add to the pool, respectively. Also, you cannot add any more to any of the variables if the pool is empty. It would look sort of like this:

Pool: 34

Alpha: (3) + –

Beta: (0) + –

Gamma: (21) + –

Delta: (9) + –

The pluses and minuses are images with an onclick event, so eventually i can animate the buttons and customize them how i want with photoshop and flash.

I was hoping there would be a simple way to output an integer variable right into the page, without having it in a form box, but have yet to find a syntax to do that. That’s why i’ve been using the <span> element. However, my function ignores the pool being equal or less than 0 because it doesn’t recognize the element as an integer, even when i use parseInt… i’m stumped. Here’s some basic code:

[code]
<html>
<head>

<script type=”text/javascript”>

function AddDP(a)
{
var temp = parseInt(a);
if(a <= 0)
{
return(0);
}
else
{
var DP = document.getElementById(“DP”);
DP.innerHTML = parseInt(DP.innerHTML) – 1;
return(1);
}

}

function SubDP(a)
{
var temp = parseInt(a);
if(temp <= 0)
{
return(0);
}
else
{
var DP = document.getElementById(“DP”);
DP.innerHTML = parseInt(DP.innerHTML) + 1;
return(-1);
}

}

</script>

<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″><style type=”text/css”>
<!–
body {
background-color: #000000;
}
body,td,th {
color: #FFCC33;
}
–>
</style></head>

<body>

Pool: <span id=’DP’>60</span>

<br>
<br>

Chill <img onclick=’
var Chill = document.getElementById(“Chill”);
var DP = document.getElementById(“DP”);
Chill.innerHTML = parseInt(Chill.innerHTML) + AddDP(Chill);’
src=Plus.jpg>
<img onclick=’
var Chill = document.getElementById(“Chill”);
var DP = document.getElementById(“DP”);
Chill.innerHTML = parseInt(Chill.innerHTML) + SubDP(Chill);’
src=Minus.jpg>
(<span id=’Chill’>0</span>)
</body>
</html>
[/code]

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@LazerAug 01.2006 — Hi

in the adddp change the <=0 to ==0 because it will never be less than 0.

If you parse variables you do not needd the parseint, remove it.

But you are changing the content of a variable with itself....

use diferent variables to change other ones.

and I did not get what EXACTLY are you trynig to do?

It looks like some kind of a math game to me...

is it?
Copy linkTweet thisAlerts:
@KorAug 01.2006 — take care with parseInt(). It is designed to parse numbers from another base to decimal, so that, if do not specify that another base, "weird" things might occure

alert(parseInt('08'))// parser will return 0, as the interpretor thinks the string is an octal, not a decimal

Either use the base argument parseInt(string,[B]10[/B]), or better use the Number() method instead.
Copy linkTweet thisAlerts:
@DudeclesauthorAug 02.2006 — I'll try to be more clear. I saw another one of your posts which brought up the Number() method. I tried using that but it didn't seem to have an effect.

Anyway, I changed the code a bit, but still can't get it to work. It is supposed to be a skill tree for a game, that you can place points into. So let's say there are 3 skills, and you have 50 points (the pool) to distribute among them . Each skill has a "plus" and a "minus" button next to it. pressing the "plus" button should add one to the skill, and subtract one from the pool. The "subtract" button does the opposite. However, if you are trying to add to a skill and the pool is empty, it should do nothing. similarly, you cannot subtract points out of a skill beyond 0... in otherwords, none of the variable should ever be a negative value. I added some comments, so you'll know sort of what i'm trying to do (even though i'm probably nowhere close).

<i>
</i>&lt;html&gt;
&lt;head&gt;

&lt;script type="text/javascript"&gt;



function AddDP(a, b)
{
a = Number(a); //make sure it's a number
if(a == 0)
{
return(0); //will not change anything
}
else
{
var DP = document.getElementById("DP"); //grab value of skill pool
if(b == TRUE) // true indicates this is for adding to the skill
{
DP.innerHTML = Number(DP.innerHTML) - 1; //subtract one from pool
return(1); //add one to skill
}
if(b == FALSE) // false means this is for the minus button
{
DP.innerHTML = Number(DP.innerHTML) + 1; //add one to the pool
return(-1); //subtract one from the skill
}
}

}


&lt;/script&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;&lt;style type="text/css"&gt;
&lt;!--
body {
background-color: #000000;
}
body,td,th {
color: #FFCC33;
}
--&gt;
&lt;/style&gt;&lt;/head&gt;

&lt;body&gt;


Pool: &lt;span id='DP'&gt;9&lt;/span&gt; &lt;!--//initialize value of pool within a span--&gt;


&lt;br&gt;
&lt;br&gt;


&lt;!-- the plus button --&gt;

Chill &lt;img onclick='
var Chill = document.getElementById("Chill"); <br/>
&lt;!--//grab current value of skill--&gt;
var DP = document.getElementById("DP"); <br/>
&lt;!--//grab current value of pool--&gt;
Chill.innerHTML = Number(Chill.innerHTML) + AddDP(DP, TRUE);'
&lt;!--//value of skill is equal to itself + result of function.--&gt; <br/>
src=Plus.jpg&gt;


(&lt;span id='Chill'&gt;0&lt;/span&gt;) // display value of skill




&lt;/body&gt;
&lt;/html&gt;


also, what should i be using for <script type=" ">? a tutorial i was using always used "text/javascript" ... they also didn't have any examples manipulating and displaying integer variable, which is what i'm trying to do.

I thought there would have been a way to display a numbervariable right in the text, like: println("Hi, it has been " + days + " since you last visited");

does this not exist?
Copy linkTweet thisAlerts:
@KorAug 02.2006 — javascript is case sensitive. Boolean values are lowercase: [B]true[/B], [B]false[/B]. But if a variable has as value a Boolean, there is no need to use the values, as the evaluated condition is a Boolean as well, by default.

if(b) - it is the same thing as if(b==true)

and

if(!b) - it is the same as if(b==false)
Copy linkTweet thisAlerts:
@KorAug 02.2006 — Yes

<script type="text/javascript">

Or, if all your <script> codes are javascript, you may use in HEAD a meta tag

<meta http-equiv="Content-Script-Type" content="text/javascript">
Copy linkTweet thisAlerts:
@DudeclesauthorAug 02.2006 — the TRUE FALSE wasn't the problem, right after i posted i tried with 1 and 0 instead of tre/false, just to make sure, and i just tried the if(b) if (!b), and no effect. The function still never returns 0 for a value. here is the latest complete code:

<i>
</i>&lt;html&gt;
&lt;head&gt;

&lt;script type="text/javascript"&gt;



function AddDP(a, b)
{
a = Number(a); //make sure it's a number
if(a == 0)
{
return(0); //will not change anything
}
else
{
var DP = document.getElementById("DP"); //grab value of skill pool
if(b == 1) // 1 indicates this is for adding to the skill
{
DP.innerHTML = Number(DP.innerHTML) - 1; //subtract one from pool
return(1); //add one to skill
}
if(b == 0) // 0 means this is for the minus button
{
DP.innerHTML = Number(DP.innerHTML) + 1; //add one to the pool
return(-1); //subtract one from the skill
}
}

}


&lt;/script&gt;

&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;&lt;style type="text/css"&gt;
&lt;!--
body {
background-color: #000000;
}
body,td,th {
color: #FFCC33;
}
--&gt;
&lt;/style&gt;&lt;/head&gt;

&lt;body&gt;


Pool: &lt;span id='DP'&gt;9&lt;/span&gt; &lt;!--//initialize value of pool within a span--&gt;


&lt;br&gt;
&lt;br&gt;


&lt;!-- the plus button --&gt;

Chill &lt;img onclick='
var Chill = document.getElementById("Chill"); <br/>
&lt;!--//grab current value of skill--&gt;
var DP = document.getElementById("DP"); <br/>
&lt;!--//grab current value of pool--&gt;
Chill.innerHTML = Number(Chill.innerHTML) + AddDP(DP, 1);' src=Plus.jpg&gt;
&lt;!--//value of skill is equal to itself + result of function.--&gt;


&lt;!-- the minus button --&gt;

&lt;img onclick='var Chill = document.getElementById("Chill"); <br/>
&lt;!--//grab current value of skill--&gt;
var DP = document.getElementById("DP"); <br/>
&lt;!--//grab current value of pool--&gt;
Chill.innerHTML = Number(Chill.innerHTML) + AddDP(Chill, 0);' src=Minus.jpg&gt;
&lt;!--//value of skill is equal to itself + result of function.--&gt;



(&lt;span id='Chill'&gt;0&lt;/span&gt;) &lt;!-- display value of skill --&gt;




&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@KorAug 02.2006 — 
Chill <img onclick='

var Chill = document.getElementById("Chill");


<!--//grab current value of skill-->

var DP = document.getElementById("DP");


<!--//grab current value of pool-->

Chill.innerHTML = Number(Chill.innerHTML) + AddDP(DP, TRUE);'

<!--//value of skill is equal to itself + result of function.-->


src=Plus.jpg>


(<span id='Chill'>0</span>)
[/quote]

This does not looks like a valid HTML code. Get rid of the comments and double quote the event's functions handler.
Copy linkTweet thisAlerts:
@DudeclesauthorAug 02.2006 — <i>
</i>&lt;!-- the plus button --&gt;

Chill &lt;img onclick="var Chill = document.getElementById("Chill");
var DP = document.getElementById("DP");
Chill.innerHTML = Number(Chill.innerHTML) + AddDP(DP, 1);" src=Plus.jpg&gt;



&lt;!-- the minus button --&gt;

&lt;img onclick="var Chill = document.getElementById("Chill");
var DP = document.getElementById("DP"); <br/>
Chill.innerHTML = Number(Chill.innerHTML) + AddDP(Chill, 0);" src=Minus.jpg&gt;

(&lt;span id='Chill'&gt;0&lt;/span&gt;) &lt;!-- display value of skill --&gt;


now it doesn't do anything, clicking on the buttons has no effect.

Isn't there a function like java's println(); that is dynamic?

I feel like there's a big javascript conspiracy preventing me from doing this.... i never imagined it would be so hard to output a number variable.
×

Success!

Help @Dudecles spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...