So I currently have a set up where there are input boxes that take numbers. The range is 0-100. I want to keep the value within that range, so if it's set to like, 200, it just caps at 100, or 0 in the case of a negative number.
I figure it'd be possible with an if statement. But the only way I see it working is through multiple if/else if statements. So I'm wondering if there's some other method to accomplish the goal.
Can view the current thing here
My code:
function updateStats() {
var hp = document.getElementById("hpValue").value;
var mp = document.getElementById("mpValue").value;
var strength = document.getElementById("strengthValue").value;
var defense = document.getElementById("defenseValue").value;
var speed = document.getElementById("speedValue").value;
var luck = document.getElementById("luckValue").value;
document.getElementById("hpBar").style.width = hp + "%";
document.getElementById("mpBar").style.width = mp + "%";
document.getElementById("strBar").style.width = strength + "%";
document.getElementById("defBar").style.width = defense + "%";
document.getElementById("speedBar").style.width = speed + "%";
document.getElementById("luckBar").style.width = luck + "%";
}
table {
border-collapse: collapse;
}
table,
th,
td {
border: 1px solid black;
}
.statbarsBG {
background-color: #808080;
text-align: left;
height: 20px;
width: 350px;
-webkit-appearance: none;
border: none;
font-weight: bold;
/* Set the progressbar to relative */
position: relative;
}
.statbarsHP {
background-color: green;
height: 20px;
}
.statbarsMP {
background-color: purple;
height: 20px;
}
.statbarsSTR {
background-color: red;
height: 20px;
}
.statbarsDEF {
background-color: darkblue;
height: 20px;
}
.statbarsSPD {
background-color: powderblue;
height: 20px;
}
.statbarsLUCK {
background-color: khaki;
height: 20px;
}
input:invalid {
border: 3px solid red;
}
<h3>Character Rank: B</h3>
<table cellspacing="20">
<tr>
<th>Stat</th>
<th> Level</th>
<th> Value (Between 0-100)</th>
</tr>
<tr>
<td>HP</td>
<td>
<div class="statbarsBG">
<div class="statbarsHP" style=" width:93%" id="hpBar" </div>
</td>
<td> <input type="text" pattern="[0-9]+" id="hpValue"> </td>
</tr>
<tr>
<td label="MP">MP</td>
<td>
<div class="statbarsBG">
<div class="statbarsMP" style=" width:30%" id="mpBar">
</div>
</div>
</>
</td>
<td> <input type="text" pattern="[0-9]+" id="mpValue" value=""> </td>
</tr>
<tr>
<td>STR </td>
<td>
<div class="statbarsBG">
<div class="statbarsSTR" style=" width:75%" id="strBar" </div>
</td>
<td> <input type="text" pattern="[0-9]+" id="strengthValue" value=""> </td>
</tr>
<tr>
<td>DEF </td>
<td>
<div class="statbarsBG">
<div class="statbarsDEF" style=" width:89%" id="defBar" </div>
</td>
<td> <input type="text" pattern="[0-9]+" id="defenseValue"> </td>
</tr>
<tr>
<td>SPEED </td>
<td>
<div class="statbarsBG">
<div class="statbarsSPD" style=" width:45%" id="speedBar" </div>
</td>
<td> <input type="text" pattern="[0-9]+" id="speedValue" value=""> </td>
</tr>
<tr>
<td>LUCK </td>
<td>
<div class="statbarsBG">
<div class="statbarsLUCK" style=" width:95%" id="luckBar" </div>
</td>
<td> <input type="text" pattern="[0-9]+" id="luckValue" value="" class="userInput"> </td>
</tr>
</table>
<button onclick="updateStats()">Click to Update Stats</button>