The 'correct' way to do what you're doing is something like:
Code:
<script type="text/javascript">
String.prototype.startsWith = function(s) {
return this.indexOf(s) === 0;
};
var photoCaptionIDs = ["13006",
"24018",
"13002",
"24065",
"24021",
"24005"],
captionIDs = ["13006, Item 1",
"24018, Item 2",
"13002, Item 3",
"24065, Item 4",
"24021, Item 5",
"24005, Item 6"];
for (var i = 0; i < captionIDs.length; ++i)
if (captionIDs[i].startsWith(photoCaptionIDs[i]))
document.getElementById("photoCaptionID" + i).innerHTML = captionIDs[i];
</script>
However, this is still a terrible way to do it: parallel arrays are horrible to work with, and innerHTML is non-standard and should be avoided where possible (and it's overkill here, anyway, since there is no HTML). What you really want to be doing is something like this:
Code:
<script type="text/javascript">
Array.map = function(f, a) {
for (var i = a.length - 1, r = []; i >= 0; --i)
r[i] = f(a[i], i);
return r;
};
String.prototype.startsWith = function(s) {
return this.indexOf(s) === 0;
};
var Dom = function() {
function clear(el) {
while (el.hasChildNodes())
el.removeChild(el.firstChild);
return el;
}
function setText(el, text) {
return clear(el).appendChild(document.createTextNode(text)).parentNode;
}
return {
clear: clear,
setText: setText
};
}();
var photoCaptions = [
["13006", "Item 1"],
["24018", "Item 2"],
["13002", "Item 3"],
["24065", "Item 4"],
["24021", "Item 5"],
["24005", "Item 6"]
];
Array.map(function(v, k) {
Dom.setText(document.getElementById("photoCaptionID" + k), v[0] + ", " + v[1]);
}, photoCaptions);
</script>
Bookmarks