I don't quite understand what you're doing, but here's some comments on your code:
> var samplearray = new Array();
> var Guy1 = new Object();
> Guy1.name = "Bill";
> Guy1.health = 100;
> samplearray.push(Guy1);
> Guy2.name = "Dan";
> Guy2.health = 125;
> samplearray.push(Guy2);
It is considered better style (and a bit tidier) to use object and array initialisers*:
var guy1 = {name: "Bill", health: 100};
var guy2 = {name: "Dan", health: 125};
var samplearray = [guy1, guy2]
Also, variable names starting with a capital letter are, by convention, reserved for constructor functions.
> //this is all done previously by a function on pageload
You need to wait for elements to be available before interacting with them, waiting for the load event is one way of doing that.
> function afunction(id) {
What is id? You seem to treat it like an object later.
Ah, so id is a reference to an element, and id.id
should return the element id.
> for (item in samplearray) {
You should declare variables so they do not become globals, so:
for (var item in samplearray) {
It's generally not a good idea to use for..in with an array because the order that members are returned may be different to their index order. Also, it will return all enumerable properties, including those on the prototype chain so you should guard against that if it's not what you want. Much better to use a for loop so you can guarantee the order and avoid non–numeric enumerable properties:
var item;
for (var i=0, iLen=samplearray.length; i<iLen; i++) {
item = samplearray[i];
> if (item == id.id){
So item will be a reference to an Object member of samplearray and id.id
is a string so this will always return false, none of the following code will run.
> document.getElementById("changeme").innerHTML=item.name;
In the for..in version, item is a string property name, but you are treating it like an object so this will throw an error and script execution will stop.
In the for loop version, it's a reference to one of the objects in samplearray so the above should "work".
> document.getElementById("changeme").innerHTML=samplearray[item].name;
This should have worked provided item was a numeric property name and not some other enumerable property.
> //neither does this
> } }}
*
Intialiser is a general term for an expression that creates an object (such as an Object, Array, Regular Expression, etc.). Where the initialiser uses literal values it may be called a "literal".