Solved

Get all elements names via javascript

Posted on 2001-08-09
13
3,137 Views
Last Modified: 2013-11-19
hi guys, can someone help me during my problem.
I want to cek all element that in my html page from javascript without make a loop statement ..

<html>
<body>
<form name="test" action="" method="post">
<tr>
  <td>name :</td>
  <td><input name="text1"></td>
</tr>
<tr>
  <td>ID Card :</td>
  <td><input name="text2"></td>
</tr>
</form>
</body>
</html>

in this page i have 3 elements : test, text1, text2
i wonder that there is some method to get this all emelents without loop .. ( if there is another script will be oke too like vbscript or else ...) ..

thanks ...
0
Comment
Question by:jbrizal
  • 7
  • 6
13 Comments
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6370963
try this

<html>
<head>
<script language="javascript">
     function getChildren(x) {
          var allChildren = x.tags("INPUT");
          alert(allChildren[0].name);
     }
</script>
</head>
<body>
<form name="test" action="" method="post">
<table>
<tr>
 <td>name :</td>
 <td><input name="text1"></td>
</tr>
<tr>
 <td>ID Card :</td>
 <td><input name="text2"></td>
</tr>
</table>
<input type=button value="get children" onclick="getChildren(test)">
</form>
</body>
</html>

nzjonboy
0
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6370971
further to my earlier post

the line var allChildren = x.tags("INPUT"); gets all the input tags within the test form. you can assign all of these to one variable but to get out put you will need to loop, something like this

for (var i =0; i<allChildren.length; i++) {
  alert(allChildren[i].name);
}

nzjonboy
0
 

Author Comment

by:jbrizal
ID: 6370982
hi .. nice try nzjonboy

but i think your method not get all this html elements names and beside that i want maybe some elements not only INPUT but can be OPTION or SELECT elements.. (not the simple like that:P ) . .. anyway thank you ..

p/s : some body can help me using DOM methot like findElements or getElements ??

0
 

Author Comment

by:jbrizal
ID: 6370984
hi .. nice try nzjonboy

but i think your method not get all this html elements names and beside that i want maybe some elements not only INPUT but can be OPTION or SELECT elements.. (not the simple like that:P ) . .. anyway thank you ..

p/s : some body can help me using DOM methot like findElements or getElements ??

0
 

Author Comment

by:jbrizal
ID: 6370990
hi .. nice try nzjonboy

but i think your method not get all this html elements names and beside that i want maybe some elements not only INPUT but can be OPTION or SELECT elements.. (not the simple like that:P ) . .. anyway thank you ..

p/s : some body can help me using DOM methot like findElements or getElements ??

0
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6371007
here you go, it also gets the button at the bottom so you may want to test the code or shorten the loop by 1

<html>
<head>
<script language="javascript">
     function getChildren(x) {
          var allChildren = x.elements;
          for(var i=0; i<allChildren.length; i++) {
               alert(allChildren[i].name + " " + allChildren[i].value);
          }
     }
</script>
</head>
<body>
<form name="test" action="" method="post">
<table>
<tr>
 <td>name :</td>
 <td><input name="text1"></td>
</tr>
<tr>
 <td>ID Card :</td>
 <td><input name="text2"></td>
</tr>
<tr>
 <td>ID Card :</td>
 <td><select name="select1">
     <option value="1" selected>1</option>
     <option value="2">2</option>
     <option value="3">3</option>
     </select></td>
</tr>
</table>
<input type=button value="get children" onclick="getChildren(test)">
</form>
</body>
</html>

nzjonboy
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:nzjonboy
ID: 6371024
you could replace the x.elements with x.all so that you get everything. elements will only return button, input, select and textarea elements from within the form whereas all returns everything. the only problem here is that you get EVERYTHING, even if you have not given it a name. if you want an example of this I'll post the code.

nzjonboy

0
 

Author Comment

by:jbrizal
ID: 6371043
hi .. nzjonboy
i think your method is .. good la ..but i try to find another way without using loop .. like HTML DOM method because the DOM have function to get this element just call the function ..

so .. i'll wait for another method ya ..


0
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6371069
the example I have given you does get all the elements without using a loop. the loop is only ther to get the values out. this has to be done becuase if you don't and you simply have something like alert(allChildren); then the text [object] will be displayed to you. sorry but there is no way around this. using the DOM get element functions you still need the index value of the element that you want therefore still need to get length and unless you know what number the element is then looping through is the only way.

nzjonboy
0
 

Author Comment

by:jbrizal
ID: 6371090
well .. nzjonboy

if you can show me the DOM call function getElements() from javascript ..  to get the name all elements you will got this point .. (DOM function .. not the javascript only ya ) ... thx ..
0
 
LVL 3

Expert Comment

by:nzjonboy
ID: 6371108
0
 
LVL 3

Accepted Solution

by:
nzjonboy earned 200 total points
ID: 6371114
0
 

Author Comment

by:jbrizal
ID: 6371126
well .. nzjonboy .. oke la .. you have give me a best shoot .. :P .. this point belongs to you .. well done .. even not a 100% answer my 1st question ya .. i hope there is new method to support this ya ..  thx .

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now