sanchopansa
08-01-2006, 05:47 PM
What I have to do is to fill the content of the second dropdown based on the choice in the first one, and the content of the third dropdown based on the second. My problem is that only the first thing works. When I try to fill the third dropdown it won't work. I'm using the same function for both so it is really strange.
This is the js/ajax part:
var http = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
var toFill, flag, x, elems;
if(http)
{
http.onreadystatechange = function ()
{
if(http.readyState == 4)
{
if (http.status == 200)
{
alert(flag);
toFill.disabled = false;
clean(toFill);
elems = eval('('+http.responseText+')');
for(x in elems)
{
var newElem = document.createElement('option');
if(flag == 1)
{
newElem.text = elems[x];
}
if(flag == 2)
{
newElem.text = elems[x]['name'];
}
newElem.value = x;
toFill.appendChild(newElem);
}
}
}
};
} else
{
alert('Error initializing XMLHttpRequest!');
}
function clean(obj)
{
for(var x=obj.options.length-1; x>=0 ; x--)
{
obj.remove(0);
}
var newElem = document.createElement('option');
obj.appendChild(newElem);
}
function fillSelect(obj)
{
if(!obj.options[0].value)
{
obj.remove(0);
}
if(obj.id == 'county')
{
flag = 1;
var itmade = 'area';
var folder = 'county';
document.getElementById('imagesel').disabled = true;
}
if(obj.id == 'area')
{
flag = 2;
var itmade = 'imagesel';
var folder = 'area';
}
toFill = document.getElementById(itmade);
http.open('GET', folder+'/'+obj.value+'.json', true);
http.send(null);
}
function changePic(obj)
{
if(!obj.options[0].value)
{
obj.remove(0);
}
var img = document.getElementById('img_holder');
var tmp = elems[obj.value];
img.src = 'img/'+tmp['filename'];
img.alt = tmp['name'];
document.getElementById('desc').innerText = tmp['desc'];
}
This is my html:
<form action="">
<div id="select_top" class="select_top">
<label for="county">County:</label>
<select id="county" class="county" onchange="fillSelect(this);" >
<option></option>
<option value="0">BGMountains</option>
<option value="1">Seaside</option>
<option value="2">BigCities</option>
<option value="3">CentralBulgaria</option>
</select><br />
</div>
<div id="select_middle" class="select_middle" >
<label for="area">Area:</label>
<select id="area" class="area" onchange="fillSelect(this);" disabled="disabled" >
<option></option>
</select>
</div>
<div id="select_bottom" class="select_bottom">
<label for="imagesel">Image:</label>
<select id="imagesel" class="imagesel" onchange="changePic(this);"disabled="disabled" >
<option></option>
</select>
</div>
</form>
<div id="holder" class="holder">
<img id="img_holder" src="" alt="" />
</div>
<div id="history" class="history">
<p>Image description</p>
<div id="desc"></div>
</div>
The only clue that I have so fat is that the part that should work on the response (the top part of js/ajax source) is not executed the second time fillSelect() is called. Can anyone help me fix this annyoing problem?
This is the js/ajax part:
var http = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
var toFill, flag, x, elems;
if(http)
{
http.onreadystatechange = function ()
{
if(http.readyState == 4)
{
if (http.status == 200)
{
alert(flag);
toFill.disabled = false;
clean(toFill);
elems = eval('('+http.responseText+')');
for(x in elems)
{
var newElem = document.createElement('option');
if(flag == 1)
{
newElem.text = elems[x];
}
if(flag == 2)
{
newElem.text = elems[x]['name'];
}
newElem.value = x;
toFill.appendChild(newElem);
}
}
}
};
} else
{
alert('Error initializing XMLHttpRequest!');
}
function clean(obj)
{
for(var x=obj.options.length-1; x>=0 ; x--)
{
obj.remove(0);
}
var newElem = document.createElement('option');
obj.appendChild(newElem);
}
function fillSelect(obj)
{
if(!obj.options[0].value)
{
obj.remove(0);
}
if(obj.id == 'county')
{
flag = 1;
var itmade = 'area';
var folder = 'county';
document.getElementById('imagesel').disabled = true;
}
if(obj.id == 'area')
{
flag = 2;
var itmade = 'imagesel';
var folder = 'area';
}
toFill = document.getElementById(itmade);
http.open('GET', folder+'/'+obj.value+'.json', true);
http.send(null);
}
function changePic(obj)
{
if(!obj.options[0].value)
{
obj.remove(0);
}
var img = document.getElementById('img_holder');
var tmp = elems[obj.value];
img.src = 'img/'+tmp['filename'];
img.alt = tmp['name'];
document.getElementById('desc').innerText = tmp['desc'];
}
This is my html:
<form action="">
<div id="select_top" class="select_top">
<label for="county">County:</label>
<select id="county" class="county" onchange="fillSelect(this);" >
<option></option>
<option value="0">BGMountains</option>
<option value="1">Seaside</option>
<option value="2">BigCities</option>
<option value="3">CentralBulgaria</option>
</select><br />
</div>
<div id="select_middle" class="select_middle" >
<label for="area">Area:</label>
<select id="area" class="area" onchange="fillSelect(this);" disabled="disabled" >
<option></option>
</select>
</div>
<div id="select_bottom" class="select_bottom">
<label for="imagesel">Image:</label>
<select id="imagesel" class="imagesel" onchange="changePic(this);"disabled="disabled" >
<option></option>
</select>
</div>
</form>
<div id="holder" class="holder">
<img id="img_holder" src="" alt="" />
</div>
<div id="history" class="history">
<p>Image description</p>
<div id="desc"></div>
</div>
The only clue that I have so fat is that the part that should work on the response (the top part of js/ajax source) is not executed the second time fillSelect() is called. Can anyone help me fix this annyoing problem?