0

I have researched and have been trying to figure this problem out for a few days. Haven't found anything that really helps other than make it more complex. I'm trying to change the variable health, based on what level their character is which they can choose from a drop down menu. I can't figure out how to do the javascript function and call the answer that they have in the drop down menu. Any help will be greatly appreciated! Here's my code:

<!DOCTYPE html>
<html>
<head>League Of Legends Damage Calculator<title>
</title>
</head>
<body>

<p>
<script>
var health;
var ad;
var adspeed;
var movespeed;
var healthregen;
var armor;
var apresist;
</script>
</p>

<p>
Aatrox Base Stats: <br>
<img src="http://ddragon.leagueoflegends.com/cdn/4.5.4/img/champion/Aatrox.png"     width="150" height="150"> <br>
<script>
document.write("Health - " + health);
</script>
Basic Attack Damage - 55 
Basic Attack Speed - .651 
Movement Speed - 345 
Health Regen - 5.75 
Armor - 18 
Magic Resist - 40 
</p>
<p>
<form name="form1">
    <select id="opts" onchange="showForm()">
        <option value="0">Level Stats - 1</option>
        <option value="1">Level Stats - 2</option>
        <option value="2">Level Stats - 3</option>
        <option value="3">Level Stats - 4</option>
        <option value="4">Level Stats - 5</option>
        <option value="5">Level Stats - 6</option>
        <option value="6">Level Stats - 7</option>
        <option value="7">Level Stats - 8</option>
        <option value="8">Level Stats - 9</option>
        <option value="9">Level Stats - 10</option>
        <option value="10">Level Stats - 11</option>
        <option value="11">Level Stats - 12</option>
        <option value="12">Level Stats - 13</option>
        <option value="13">Level Stats - 14</option>
        <option value="14">Level Stats - 15</option>
        <option value="15">Level Stats - 16</option>
        <option value="16">Level Stats - 17</option>
        <option value="17">Level Stats - 18</option>
    </select>
</form>

<script>
function Stats(form1) {
    var decidelevel;
    decidelevel = document.getElementById('opts');
if(decidelevel = "0"){
    health = 395;
};
if(decidelevel = "1") {
    health = 395 + (85 * decidelevel);
};
// etc
};
</script>
</p>

</body>
</html>
B3nj117
  • 3
  • 1

2 Answers2

0

You've got a couple problems: First, the <select> is calling showForm() when it changes, but I don't see a function named showForm in your javascript. You should change that to stats(). Then, you're properly doing decidelevel=document.getElementById('opts');, but you need to go a step further: decidedlevel=document.getElementById('opts').value; will do it.

EDIT:

An easier way to write all those if statements would be using a switch:

switch(decidedlevel){
    case 0:
        //do stuff
    break;
    case 1:
        //do other stuff
    break;
    //etc
    default:
        //if none match, this happens
    break;
}

EDIT2: PS:

You are also doing your if statement's incorrectly - if (var=1) will assign var to 1. Instead, use if (var==1) to get a true/false.

Cheers!

Helpful
  • 702
  • 4
  • 16
  • That fixed my problem with calling the variable and the switch statement helped save ALOT of room, But it still replaces my health variable with undefined. Any idea how to fix that? – B3nj117 Apr 18 '14 at 03:16
  • It looks like you're trying to insert the variable 'health' into the DOM before you assign anything to it - see: `` To do it properly, I'd suggest replacing that entire ` – Helpful Apr 18 '14 at 03:25
  • Ok, so now it doesn't say undefined, but it doesn't show the numbers. When you say madeUpTag, i literally wrote madeUpTag. I don't know if that's what you meant or not. I really appreciate your help on this. – B3nj117 Apr 18 '14 at 03:36
  • Did you remember to include the `document.getElementsByTagName("madeUpTag")[0].innerHTML="Health: "+health;`? If so, try replacing the part after the `=` with `"Testing the function";` to be sure it's working. If that works, then `health` probably still isn't assigned a value. – Helpful Apr 18 '14 at 03:42
0

In the onchange event you're calling showForm() function which does not exist. You should change it to Stats();

<select id="opts" onchange="Stats()">

Then inside the Stats() function, you should retrieve the selected index by calling

decidelevel = document.getElementById('opts').value;

Also in the if condition inside Stats() function you should use == or ===

if(decidelevel === "0"){

See Working example below

<!DOCTYPE html>
<html>
<head>League Of Legends Damage Calculator<title>
</title>
</head>
<body>

<p>
<script>
var health;
var ad;
var adspeed;
var movespeed;
var healthregen;
var armor;
var apresist;
</script>
</p>

<p>
Aatrox Base Stats: <br>
<img src="http://ddragon.leagueoflegends.com/cdn/4.5.4/img/champion/Aatrox.png"     width="150" height="150"> <br>
<script>
<!--document.write("Health - " + healthregen); -->
</script>
Basic Attack Damage - 55 
Basic Attack Speed - .651 
Movement Speed - 345 
Health Regen - <span id="health">5.75</span> 
Armor - 18 
Magic Resist - 40 
</p>
<p>
<form name="form1">
    <select id="opts" onchange="Stats()">
        <option value="0">Level Stats - 1</option>
        <option value="1">Level Stats - 2</option>
        <option value="2">Level Stats - 3</option>
        <option value="3">Level Stats - 4</option>
        <option value="4">Level Stats - 5</option>
        <option value="5">Level Stats - 6</option>
        <option value="6">Level Stats - 7</option>
        <option value="7">Level Stats - 8</option>
        <option value="8">Level Stats - 9</option>
        <option value="9">Level Stats - 10</option>
        <option value="10">Level Stats - 11</option>
        <option value="11">Level Stats - 12</option>
        <option value="12">Level Stats - 13</option>
        <option value="13">Level Stats - 14</option>
        <option value="14">Level Stats - 15</option>
        <option value="15">Level Stats - 16</option>
        <option value="16">Level Stats - 17</option>
        <option value="17">Level Stats - 18</option>
    </select>
</form>

<script>
function Stats(form1) {
    var decidelevel=0;
    decidelevel = document.getElementById('opts').value;
switch(decidelevel){
    case "0":
        healthregen = 100;
    break;
    case "1":
        healthregen = 200;
    break;
    default:
        healthregen = 999;
    break;
}

document.getElementById('health').innerHTML = healthregen;

};

</script>
</p>

</body>
</html>
Community
  • 1
  • 1
Nipuna
  • 6,846
  • 9
  • 64
  • 87