/    Sign up×
Community /Pin to ProfileBookmark

How to count for object

Hi, everyone.
I have a small problem about javascript.. I have a form and inside the form. there’s a object, say, <textbox ID=textBox /> and this object is generated by ASP. so it depends on the loop. it can generate multple textbox with different ID. eg: <textbox ID=textBox1 />, <textbox ID=textBox2 />, <textbox ID=textBox3 />

Now, I somehow know, but unsure, that javascript can count the number of object (textbox in this case). can someone tell me how?

Many thanks
Carl

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@SamFeb 20.2004 — not sure how crossbrowser this is, but this should work in IE.
[code=php]
<head>
<script type="text/javascript">
function test()
{
alert(document.getElementById('form1').children.length)
}
</script>
</head>

<body onload="test();">
<form id=form1>
<input type="text" id=textBox1>
<input type="text" id=textBox2>
<input type="text" id=textBox3>
</form>
</body>
[/code]
Copy linkTweet thisAlerts:
@KorFeb 21.2004 — Here's a crossbrowser solution. More than that, It will show you only the lenght of the input type=text, as you may have other types of inputs in your form.

[code=php]
<html>
<head>
<script>
function f(){
d = document.forms[0];
x = 0;
t = new Array()
for (i=0;i<d.elements.length;i++){
t[i]= d.elements[i].type;
if(t[i] == 'text'){
x++;
}
}
alert('There are ' +x+ ' input type=text elements in form');
}
</script>
</head>

<body onload="f()">
<form>
<input name="" type="checkbox" value="">
<input type="text" id="textBox1">
<input type="text" id="textBox2">
<input type="text" id="textBox3">
</form>
</body>
</html>
[/code]
×

Success!

Help @Carl 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 6.2,
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: @meenaratha,
tipped: article
amount: 1000 SATS,

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

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...