So this is one of first attempts at some AJAX stuff - and it works!  However, 
it's far from elegant, so I was hoping someone could help me do what I'm 
assuming is possible.

The basic idea:  There are tons of links that, when clicked, don't refresh the 
page, but instead use AJAX to populate 22 table cells with information.

Each td to be populated is id'd as "myCom_"varName  - example (myCom_name)


The Code:
----------------------------------------------------

Simple simple simple XML document:

<community>
<name>Winchester</name>
<state>NH</state>
.....
<college_20>11111</college_20>
</community>





JavaScript:

var varNames = new Array(22);
varNames[0] = 'name';
varNames[1] = 'state';
.....
varNames[21] = 'college_10';



Then there's your standard AJAX stuff and we get to:
response = req.responseXML.documentElement;



I found that these next two lines of code populate one cell of my table:
name = response.getElementsByTagName('name')[0].firstChild.data;
document.getElementById('myCom_name').innerHTML = name;


Rather than repeat them 22 times, I wanted to make a loop.  So...

for (var i = 0; i < varNames.length; i++) {
                myCode1 = varNames[i] + " = response.getElementsByTagName('" + 
varNames[i] + "')[0].firstChild.data;";
                myCode2 = "document.getElementById('myCom_" + varNames[i] + 
"').innerHTML = " + varNames[i] + ";";
                eval(myCode1);
                eval(myCode2);
}

That right there ^^ is what I think is ugly, although it does work.
1) I'm not sure how often the 'eval()' statement should be used, but I do find 
it handy when doing complicated things like that.
2) I wish I didn't have to set up an array with all the variable names ahead of 
time.
3) What I'd really like to do, would replace that loop with something like:

For each element in <community> (
   document.getElementById('myCom_' elementName).innerHTML = elementValue;
)



Is there an easy way to step through the XML document in JavaScript and get the 
kind of loop I want?  

Thanks much.




~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~|
Find out how CFTicket can increase your company's customer support 
efficiency by 100%
http://www.houseoffusion.com/banners/view.cfm?bannerid=49

Message: http://www.houseoffusion.com/lists.cfm/link=i:5:179363
Archives: http://www.houseoffusion.com/cf_lists/threads.cfm/5
Subscription: http://www.houseoffusion.com/lists.cfm/link=s:5
Unsubscribe: 
http://www.houseoffusion.com/cf_lists/unsubscribe.cfm?user=11502.10531.5
Donations & Support: http://www.houseoffusion.com/tiny.cfm/54

Reply via email to