getting a specific element in DOM W3C

Discussion in 'Javascript' started by Ricardo Garcia, Sep 21, 2004.

  1. 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
    Ricardo Garcia, Sep 21, 2004
    #1
    1. Advertising

  2. Ricardo Garcia wrote:

    > 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.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Sep 21, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Frank
    Replies:
    9
    Views:
    1,562
    David Dorward
    May 3, 2004
  2. RC
    Replies:
    3
    Views:
    409
    Joseph J. Kesselman
    Apr 10, 2008
  3. RC
    Replies:
    3
    Views:
    550
    Joseph J. Kesselman
    Apr 10, 2008
  4. Alan
    Replies:
    6
    Views:
    1,595
  5. Adam Funk
    Replies:
    11
    Views:
    6,095
    Adam Funk
    Jan 21, 2010
Loading...

Share This Page