Hello everyone, I'm new to javascript and Hot Scripts so please excuse me if doing something dumb here. I've built a form on my website to update entries on my database. It runs a loop to create the form for each row in the database. I'm then trying to use javascript to modify one of the fields when you've made a change to the field. Right now I've got it where the javascript is recognizing a change to each of the fields but its only performing the task on the last field. Can someone point me in the right direction to fix this problem?
The field that I'm trying to change is the "imbalance" field. I'm trying to get it to multiply the entry
by 1,000. For example I would like the user to type in "35" and when they tab out of the field the value will be "35,000".
Here is my form code: this will produce a form with 7 rows from the database to be updated
<?php
$sql = "SELECT * FROM mocs ORDER BY id";
$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$numrows= mysql_num_rows($result);
echo $numrows;
$i = 0;
echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Symbol</td>';
echo '<td>Buy/Sell</td>';
echo '<td>Imbalance</td>';
echo '<td>Price</td>';
echo '</tr>';
echo "<form id='formUpdate' name='formUpdate' method='post' action='update.php'>\n";
while ($moc = mysql_fetch_array($result)) {
echo '<tr>';
echo "<td>{$moc['id']}<input type='hidden' name='id[$i]' value='{$moc['id']}' /></td>";
echo "<td><input id='symbol' type='text' size='5' name='symbol[$i]' value='{$moc['symbol']}' /></td>";
echo "<td><input id='buySell' type='text' size='5' name='buySell[$i]' value='{$moc['buySell']}' /></td>";
echo "<td><input id='imbalance[$i]' type='text' size='10' name='imbalance[$i]' value='{$moc['imbalance']}' /></td>";
echo "<td><input id='price' type='text' size='10' name='price[$i]' value='{$moc['price']}' /></td>";
echo '</tr>';
++$i;
}
echo '<tr>';
echo "<td><input type='submit' value='submit' /></td>";
echo '</tr>';
echo "</form>";
echo '</table>';
my javascript code is:
for (i=0; i<7; i++){
var imb="imbalance[" + i + "]";
var addK = document.getElementById(imb);
console.log(addK);
addK.onchange=function(){
if (addK.value !== ""){
addK.value=addK.value*10;
}
};
};