/    Sign up×
Community /Pin to ProfileBookmark

DOM Accessing Checkboxes in a Span

So I have a span which inside it has anchors, images, and checkboxes. Is there an easy way to get an array of all the checkboxes inside that span?

I tried stuff like:

[code]
branch = document.getElementById(branchid);
for (i in branch.children) {
alert(branch.children[i].nodeName);
}
[/code]

With the idea that I can test to see if node name is an input. Yet this alert doesn’t do anything. I think my use of DOM is incorrect and that’s what I would like help with. But more importantly, is there an easier way to do get access to the array of checkboxes inside a span using DOM?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@CharlesAug 14.2006 — var a = new Array(), e, i = 0
while (e = document.getElementById (branchid).getElementsByTagName ('INPUT')[i++]) {if (e.type == 'checkbox') a.push (e)}
Copy linkTweet thisAlerts:
@ktm5124authorAug 14.2006 — Doesn't work. Here's the function I have for checking leaves in a document tree structure and the function that calls it.
<i>
</i>function checkAll(branchid) {
alert(branchid);
alert('d0');
var branch = document.getElementById(branchid);
alert('d1');
alert(branch.id);
var checkboxes = new Array(), e, i = 0;
alert('d2');
while (e = document.getElementById(branchid).getElementsByTagName('INPUT')[i++]) {
if (e.type == 'checkbox') {
a.push(e);
}
}
alert('d3');
for (i in checkboxes) {
alert(i);
}
alert('d4');
}

function writeBranch() {
var branchstr = '&lt;span class="branch"&gt;';
branchstr += '&lt;span name="reveal" onClick="handleBranch('' + this.id + '')"&gt;';
branchstr += '&lt;img src="' + openImg.src + '" id="img-' + this.id + '" &gt;' + this.text;
branchstr += '&lt;/span&gt;';
branchstr += '&lt;input type="checkbox" onClick="checkAll('' + this.id + '')"&gt;';
branchstr += '&lt;/span&gt;';
branchstr += '&lt;span class="leaf" id="' + this.id + '"&gt;';
for (i in this.leaves) {
branchstr += this.leaves[i].write() + '&lt;br&gt;';
}
branchstr += '&lt;/span&gt;';
return branchstr;
}

When a checkbox is clicked, the script prints out (in order) "branch-0", "d0", "d1", "branch-0", "d2",a nd then stops. So it's stopping at your code as it did mine.
Copy linkTweet thisAlerts:
@CharlesAug 14.2006 — You need to change "a.push(e);" to "checkboxes.push(e);" but that shouldn't cause the script to terminate.
Copy linkTweet thisAlerts:
@ktm5124authorAug 14.2006 — Haha, that was stupid and silly of me. Thanks a lot for the example Charles, it works now! ;-)
×

Success!

Help @ktm5124 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.13,
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,
)...