Solved

Get all elements names via javascript

Posted on 2001-08-09
13
3,143 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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

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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

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…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

717 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