previous

page 11

next

All being well, behind the scenes the cn values in this example are 1,0,1,1,0.
So let's work on how to retrieve those values. I'm going to create a function called get_cns so create a new file called get_cns.js and see how far you can get before looking at my effort.

You probably didn't get very far because you realised that you didn't know how to get the cn attribute from an element using its id.
This is what you need: the function document.getElementById.

function get_cns() {
var arr=[];
for (var i=0; i<5; i++) {
var element=document.getElementById(i);
var cn=element.getAttribute('cn');
arr[i]=cn;
}
return arr;
}

You will have noticed that I've introduced a new JavaScript special word, return. So when we call this function we can get the answer returned to us like this:

var arr=get_cns();

Next question is: where do we put this line? We'll also need to remember to tell our index.html about this new file.

previous
next