Solved

Get all elements names via javascript

Posted on 2001-08-09
13
3,141 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
 
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery form 9 40
Date on a table 16 34
How Close unsubmited attempts 10 44
Aspx calendar pop up control 3 27
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

756 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