Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get Elements type/name

Status
Not open for further replies.

ralphtrent

Programmer
Jun 2, 2003
958
US
Hey, I am fairly new to the whole javascript client-side world. Worked with it a little bit here and there but now I am doing more with it. I have a script below that I am trying to run to get me the types and names of an element, can anyone see why I am geting [object] as the return value for "Elements" section?

Thanks
Ralph

<script language=&quot;javascript&quot; type=&quot;text/javascript&quot;>
function loops()
{
document.write(&quot;<table border=0>&quot;)
document.write(&quot;<tr><td colspan=3>Number of Forms: &quot;+document.forms.length+&quot;</td></tr>&quot;)
for(count=0;count<document.forms.length;count+=1)
{
var formNum = count+1;
document.write(&quot;<tr><td>&quot;+formNum+&quot;</td><td>Number of elements:</td><td>&quot;+document.forms[count].length+&quot; elements</td></tr>&quot;);
document.write(&quot;<tr><td></td><td>Name:</td><td>&quot;+document.forms[count].name+&quot;</td></tr>&quot;);
document.write(&quot;<tr><td></td><td>Action:</td><td>&quot;+document.forms[count].action+&quot;</td></tr>&quot;);
document.write(&quot;<tr><td></td><td>Elements:</td><td>&quot;+document.forms[count].element+&quot;</td></tr>&quot;);
document.write(&quot;<tr><td colspan=3><hr></td></tr>&quot;);
}
document.write(&quot;</table>&quot;)
}
</script>
 
I have a typo, I have document.forms[count].elements[count]. this is what i use and I get the [object] error.
 
Your code is a little confusing, but maybe you want to do something like this:

theelement = document.forms[count].elements[count]

theelementtype = theelement.type
theelementname = theelement.name

That should give you the two variables that you want, IF I understand your problem correctly.

jared@eae.net -
 
document.forms[count].elements[count] is an object. If you want the type of it, you probably need to write document.forms[count].elements[count].type. If you just want the number of elements, then document.forms[count].elements[count].length.

I have not done this myself (accessed a 'type' attribute of a form element object), but if 'type' doesn't work, try 'name'. Also, not sure you really want to do [count] for the elements index. If there are 5 elements on form 3 (for example) then all of the following exist:

document.forms[3].elements[0]
document.forms[3].elements[1]
document.forms[3].elements[2]
document.forms[3].elements[3]
document.forms[3].elements[4]

For this reason, you may want to nest another for-loop to run through the elements of the form[count].

...unless you are doing something funky I don't know about, in which case, I'll just keep to myself. :)

'hope that helps.

--Dave
 
Dave
I think I do need to nest another loop but what would my for statment look like?
for(count1=0;<count1=document.forms[count].elements[count].count1+=1)
{
theelement = document.forms[count].elements[count]
theelementtype = theelement.type
theelementname = theelement.name
document.write(&quot;<tr><td></td><td>Elements:</td><td>&quot;+theelementname+&quot;</td></tr>&quot;);
}
When I do this the script fails completely.

Thanks
Ralph
 
Ralph! Sorry I didn't see that you'd responded to this. It's only by chance that I noticed it today. If you still need help, here's a tip on the for-loop:

Code:
for(count1=0; count1<document.forms[count].elements.length; count1++) //count1++ is same as count1+=1
 {
  theelement = document.forms[count].elements[count1]; //notice using DIFFERENT indexes for forms and elements
  theelementtype = theelement.type; //unnecessary if you are not using it
  theelementname = theelement.name;
  document.write(&quot;<tr><td></td><td>Elements:</td><td>&quot;+theelementname+&quot;</td></tr>&quot;);
 }

'hope that helps. I had to refresh myself on the original question. 'sorry I missed this two days ago!

--Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top