getting a specific element in DOM W3C

R

Ricardo Garcia

hi, i´m doing a script to get the total size of the page, and i would like
to detect for example:
<input type="image" ... >

i can get all the input tag with
document.getElementsByTagName('input')

but how can i select only the ones with this attribute??

Thanks
 
M

Martin Honnen

Ricardo said:
i would like
to detect for example:
<input type="image" ... >

i can get all the input tag with
document.getElementsByTagName('input')

but how can i select only the ones with this attribute??

You could loop through the result of
var inputs = document.getElementsByTagName('input');
for (var i = 0; i < inputs.length; i++) {
if (inputs.type == 'image') {
...
}
}
With DOM Level 2 Traversal you could also use a NodeIterator with a
NodeFilter e.g.

var nodeIterator = document.createNodeIterator(
document,
NodeFilter.SHOW_ELEMENT,
function (node) {
if (node.nodeType == 1 && node.nodeName.toLowerCase() == 'input' &&
node.type == 'image') {
return NodeFilter.FILTER_ACCEPT;
}
else {
return NodeFilter.FILTER_SKIP;
}
},
true
);

var node;

while ((node = nodeIterator.nextNode())) {
// use node here e.g.
alert(node.src);
}

but so far only Opera 7.60 preview implements node iterators so it is of
no use on the web but I have mentioned it as you asked for W3C DOM methods.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top