Monday, March 28, 2011

Correct element.getElementsByTagName() implementation

//EDIT My issue was related to something else, I thought the implementation was incorrect but it actually works, thanks for the confirmation.

Looked in jQuery and prototypejs, can't seem to find the way they implement getElementsByTagName on a element (not document.getElementsByTagName).

Here's my test html:

<div id="something" style="margin: 10px 10px 10px 15px; overflow: auto; position: relative; height: 200px;">
    <div style="float: left; width: 180px; font-size: 10px; margin-bottom: 4px;">
        <label for="label_50">
            <img style="vertical-align: middle; margin-right: 3px;" src="http://web1.wow.com/i/i_ask_sm.gif"/>
            <a style="color: rgb(0, 0, 0); text-decoration: underline;" href="http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url=[URL]&title=[TITLE]" post_link="http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url=[URL]&title=[TITLE]" rel="external" target="_blank">Ask</a>
        </label>
    </div>
</div>

And my current js:

document.getElementById("something").getElementsByTagName("a");

I'm looking for the correct implementation of this because of course IE doesn't seem to work correctly with it, and I can't use a framework.

From stackoverflow
  • <div id="something" style="margin: 10px 10px 10px 15px; overflow: auto; position: relative; height: 200px;">
     <div style="float: left; width: 180px; font-size: 10px; margin-bottom: 4px;">
      <label for="label_50">
       <img style="vertical-align: middle; margin-right: 3px;" src="http://web1.wow.com/i/i_ask_sm.gif"/>
       <a style="color: rgb(0, 0, 0); text-decoration: underline;" href="http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url=[URL]&title=[TITLE]" post_link="http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url=[URL]&title=[TITLE]" rel="external" target="_blank">Ask</a>
      </label>
     </div>
    </div>
    <pre>
    <script type="text/javascript">
    var lnks = document.getElementById("something").getElementsByTagName("a");
    for (var ii = 0; ii < lnks.length; ++ii) {
     document.writeln(lnks[ii].href);
    }
    </script>
    </pre>
    

    In IE6, IE7 and IE8RC1 this outputs:

    http://myjeeves.ask.com/mysearch/BookmarkIt?v=1.2&t=webpages&url=[URL]&title=[TITLE]
    

    Does your example not actually demonstrate the problem?

    Luca Matteis : I just realized the problem is related to something else, will update my question.

0 comments:

Post a Comment