[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3150
  • Last Modified:

Get all elements names via javascript

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
jbrizal
Asked:
jbrizal
  • 7
  • 6
1 Solution
 
nzjonboyCommented:
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
 
nzjonboyCommented:
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
 
jbrizalAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
jbrizalAuthor Commented:
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
 
jbrizalAuthor Commented:
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
 
nzjonboyCommented:
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
 
nzjonboyCommented:
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
 
jbrizalAuthor Commented:
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
 
nzjonboyCommented:
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
 
jbrizalAuthor Commented:
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
 
jbrizalAuthor Commented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now