Click to See Complete Forum and Search --> : array of layers
michelle
04-21-2003, 05:03 AM
As I understand it, you can reference to a form-field-element by either the ID or as a position in the document.forms-array.
This way you can see how many form field-elements you have on a page:
document.forms.length
I want to see how many <div> I have in a page and reference to them as [0], [1], [2], etc.
<pseudo-code>
var arrLen = document.divs.length;
for (var i = 0; i < arrLen; i++) {
alert(document.divs[i].style.top);
}
</pseudo-code>
Is there an array like this for <div>?
// Michelle
gil davis
04-21-2003, 05:46 AM
This way you can see how many form field-elements you have on a page:
document.forms.lengthNot quite. That would give you the number of FORMs in the document.document.formName.lengthordocument.forms[n].lengthwould be the correct syntax.I want to see how many <div> I have in a page
In W3C DOM compliant browsers, you can use:document.getElementsByTagName("div")It will give you an array that contains all the DIVs in the document.
michelle
04-21-2003, 06:05 AM
document.getElementsByTagName("div")It will give you an array that contains all the DIVs in the document.
I figured out how I get the lenght of the array:
myarrlength = document.getElementsByTagName("div").length
But how do I access the elements in the array?
myarr = document.getElementsByTagName("div")[0]
This does not work...
// Michelle
gil davis
04-21-2003, 07:05 AM
myarr = document.getElementsByTagName("div")[0]gives you the first DIV in the page. Is that what you intended? Or did you want the whole array?myarr = document.getElementsByTagName("div");
michelle
04-21-2003, 10:20 AM
I was maybe too hasty in my reply
I figured that the ids of the divs were in the array, so I could just write like this:
myarr = document.getElementsByTagName("div");
alert(myarr[0]);
Instead I needed this:
myarr = document.getElementsByTagName("div");
alert(myarr[0].id);