Reputation: 1940
I am trying to pass a value to a global variable. This is where shdivs() is initialized:
<select id="currentq" onchange="shdivs();">
getElementById('currentq') gets div ids. The idea is that when there is a change in the select box, old div is hidden, new one is shown. However the alerts change for the div I want to be shown (qwe changes appropriately), but the 'old' var is not changed. So when I select different things from the list I get things like:
alert:SMT alert:AUS Select again
alert:SMT alert:USD
Which means that variable 'old' is not updated.
old="SMT";
function shdivs() {
alert(old);
qwe=document.getElementById('currentq').value;
alert(qwe);
document.getElementById(qwe).style.visibility="visible";
document.getElementById(old).style.visibility="hidden";
old=qwe;
}
Can someone tell me what the problem is ?
Entire HTML/PHP/JavaScript Code:
<?
echo '<script language="JavaScript">
<!--
var rate = new Array(1,';
$rows = simplexml_load_file('cron/ecbrates.xml');
foreach($rows->Cube->Cube->Cube as $rate){
if ($rate["currency"]=="BGN") echo $rate["rate"].',';
}
foreach($rows->Cube->Cube->Cube as $rate){
if ($rate["currency"]=="USD") echo $rate["rate"].',';
}
foreach($rows->Cube->Cube->Cube as $rate){
if ($rate["currency"]=="GBP") echo $rate["rate"].',';
}
foreach($rows->Cube->Cube->Cube as $rate){
if ($rate["currency"]<>"BGN" && $rate["currency"]<>"USD" && $rate["currency"]<>"GBP")
if($rate["currency"]=="ZAR") echo $rate["rate"];
else echo $rate["rate"].',';
}
echo ');
function currency_convert(origin) {
var origin_value = eval(\'document.currency.c\'+origin+\'.value\');
var euro_equivalent = rate[origin];
var v;
for (i=0; i<rate.length; i++) {
if (i!=origin) {
v = Math.round(rate[i]*origin_value/euro_equivalent*10000)/10000;
eval(\'document.currency.c\'+i+\'.value = \'+v);
}
}
return true;
}
// -->
</script>';
?>
<div style="position:relative;">
<form name="currency">
<table cellspacing="0" cellpadding="0" border="1" bordercolor="#336699">
<tr><tr>
<td><img src="flags/eur.jpg" alt="Europe" width="30" height="20"></td>
<td>EUR</td>
<td><input type="text" name="c0" value="" size="10" onKeyUp="currency_convert(0);"></td></tr><tr>
<td><img src="flags/bgn.gif" alt="USA" width="30" height="20"></td>
<td>BGN</td>
<td><input type="text" name="c1" value="" size="10" onKeyUp="currency_convert(1);"></td></tr><tr>
<td><img src="flags/usd.gif" alt="Austria" width="30" height="20"></td>
<td>USD</td>
<td><input type="text" name="c2" value="" size="10" onKeyUp="currency_convert(2);"></td></tr><tr>
<td><img src="flags/gbp.gif" alt="Belgium" width="30" height="20"></td>
<td>GBP</td>
<td><input type="text" name="c3" value="" size="10" onKeyUp="currency_convert(3);"></td></tr><tr>
<td colspan="2">
<select id="currentq" onchange="shdivs();">
<?
foreach($rows->Cube->Cube->Cube as $rate){
if ($rate["currency"]<>"BGN" && $rate["currency"]<>"USD" && $rate["currency"]<>"GBP") {
echo '<option value="'.$rate['currency'].'">'.$rate['currency'].'</option>';
}
}
?>
</select>
</td>
<td>
<?
$numb=4;
foreach($rows->Cube->Cube->Cube as $rate){
if ($rate["currency"]<>"BGN" && $rate["currency"]<>"USD" && $rate["currency"]<>"GBP") {
echo '<div style="position:absolute; top:99px; visibility:hidden;" id="'.$rate["currency"].'"><input type="text" id="'.$rate["currency"].'" name="c'.$numb.'" value="" size="10" onKeyUp="currency_convert(4);"></div>';
$numb++;
}
}
?>
</td>
</tr></table>
</form>
</div>
<script type="text/javascript">
old="SMT";
function shdivs() {
alert(old);
qwe=document.getElementById('currentq').value;
alert(qwe);
document.getElementById(qwe).style.visibility="visible";
document.getElementById(old).style.visibility="hidden";
old=qwe;
}
</script>
Upvotes: 2
Views: 1118
Reputation: 30500
The best possible explanation for this is that one of the following two lines are failing.
document.getElementById(qwe).style.visibility="visible";
document.getElementById(old).style.visibility="hidden";
Can you verify that there all the IDs referenced by your select exist in the DOM?
Move the alert to the end of the function, and see if it pops up when you run it again.
EDIT
Since you posted the entire code, I can see a potential issue.
In the code that writes the DIV, you have assign the same id to two elements. An ID should be unique:
...id="'.$rate["currency"].'"><input type="text" id="'.$rate["currency"].'" name="c'.$numb.'"...
Unrelated
Little tip for you. If you pass this
in the onchange parameters, you don't have to getElementById
on the select:
function shdivs(objcurrentq) {
qwe=objcurrentq.value;
Upvotes: 2