Solved

Get all elements names via javascript

Posted on 2001-08-09
13
3,136 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

21 Experts available now in Live!

Get 1:1 Help Now