/    Sign up×
Community /Pin to ProfileBookmark

looping through hidden objects

how can i loop through the “hidden” objects of my form and only them?
thnaks in advance
peleg

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@KorMay 24.2006 — var el = document.forms['myform'].elements;

for(var i=0;i<el.length;i++){

if(el[i].type=='hidden'){

........ do something with el[i]........

}

}
Copy linkTweet thisAlerts:
@pelegk1authorMay 24.2006 — yep did that but wanted to know if there is another way accessing only the hidden objects

thnaks laot anyway

peleg
Copy linkTweet thisAlerts:
@KorMay 24.2006 — objects can be accessed directly only by

id, name, tagName or as elements of a form or as childNodes of a parent.
Copy linkTweet thisAlerts:
@UltimaterMay 24.2006 — You could also give all of the hidden elements the same name then loop through 'em.

<i>
</i>&lt;body&gt;
&lt;form&gt;
&lt;div&gt;
&lt;input type="hidden" name="myName" value="el1"&gt;
&lt;input type="hidden" name="myName" value="el2"&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;script type="text/javascript"&gt;
var els=document.forms[0].elements.myName;
for(var i=0,l=els.length; i&lt;l; i++){
alert(els[i].value)
}
&lt;/script&gt;
&lt;/body&gt;


I doubt you will want to give all of your INPUTs the same name, so you can also do this:
<i>
</i>&lt;body&gt;
&lt;form&gt;
&lt;div&gt;
&lt;span name="myName"&gt;&lt;input type="hidden" name="we1" value="el1"&gt;&lt;/span&gt;
&lt;span name="myName"&gt;&lt;input type="hidden" name="we2" value="el2"&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;script type="text/javascript"&gt;
var els=document.getElementsByName("myName")
for(var i=0,l=els.length; i&lt;l; i++){
alert(els[i].firstChild.value)
}
&lt;/script&gt;
&lt;/body&gt;

Because IE sucks, you need to add this code in:
<i>
</i>document.getElementsByName=function(a){
var allels=document.getElementsByTagName("*");
var findings=[];
for(var i=0;i&lt;allels.length;i++){
if(allels[i].getAttribute("name")==a)findings[findings.length]=allels[i]
}
return findings
}



This works:
<i>
</i>&lt;body&gt;
&lt;form&gt;
&lt;div&gt;
&lt;span&gt;&lt;input type="hidden" name="we1" value="el1"&gt;&lt;/span&gt;
&lt;span&gt;&lt;input type="hidden" name="we2" value="el2"&gt;&lt;/span&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;script type="text/javascript"&gt;
var els=document.forms[0].getElementsByTagName("span")
for(var i=0,l=els.length; i&lt;l; i++){
alert(els[i].firstChild.value)
}
&lt;/script&gt;
&lt;/body&gt;
Copy linkTweet thisAlerts:
@KorMay 24.2006 — ... which brings you back to the same loop whithin the elements with the same [I]something[/I], using an if() conditioner (an attribute's value), which is absolutely the same thingh.
×

Success!

Help @pelegk1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...