• 0

[JS] getElementsByTagName


Question

How can I put the results of getElementsByTagName into the 'anchors' array for each of the tags in the 'myTags' array? This is what I have so far but I'm stuck. Any help would be much appreciated. :) Thanks.

var myTags = new Array("a", "abbr", "img", "p");	

var anchors = new Array()
for (var i = 0; i < myTags.length; i ++) {
	var myTag = myTags[i]; 
	var anchors = document.getElementsByTagName(myTag);
}

Link to comment
Share on other sites

11 answers to this question

Recommended Posts

  • 0

if you are putting it into the anchors array, this array must also be indexed, so just add an i after anchors in your assignment as follows. the only problem with this is that you are putting objects, different objects, into an array. so the datatypes may clash and it may not work.

var myTags = new Array("a", "abbr", "img", "p");	

var anchors = new Array()
for (var i = 0; i < myTags.length; i ++) {
	var myTag = myTags[i]; 
	var anchors[i] = document.getElementsByTagName(myTag);
}

Edited by rson451
Link to comment
Share on other sites

  • 0

Thankyou for your reply rson451. I had tried that earlier but it didn't work and I was getting this error in the 'javascript console':

Error: missing; before statement
Source File: [...]/myJsFile.js
Line: 39, Column: 13
Source Code: var anchors[i] = document.getElementsByTagName(myTag);

Anymore idea's anyone? Alternate ways of doing this? Thanks. :)

Link to comment
Share on other sites

  • 0

Try this:

var myTags = new Array("a", "abbr", "img", "p");	
var anchors = new Array()

for (var i = 0; i < myTags.length; i ++) {
	var myTag = myTags[i]; 
	anchors.concat(document.getElementsByTagName(myTag));
}

Link to comment
Share on other sites

  • 0

No luck, thanks for your effort though DecoyDuck. :) I don't know much about javascript, or the function itself, but I read this:

Although it may appear to act like an array, it is very important to remember that the returned object is actually an HTMLCollection. It does not support any array-like functions (concat, splice, etc...)

So I don't know if that would've worked anyway? :s

Link to comment
Share on other sites

  • 0

Try:

var myTags=['a','abbr','img','p'],tag,tags,anchors=[],i;
while((tag=myTags.pop())){
  tags=document.getElementsByTagName(tag);
  if(tags.length){
	i=tags.length;
	while(i--){
	  anchors.push(tags[i]);
	}
  }
}

This should get all the requested tags present into the anchors array (which is not a HTMLCollection).

Link to comment
Share on other sites

  • 0

I don't think it has to be so difficult. I'll evaluate the following code first:

var myTags = new Array("a", "abbr", "img", "p");	

var anchors = new Array()
for (var i = 0; i < myTags.length; i ++) {
	var myTag = myTags[i];
	var anchors[i] = document.getElementsByTagName(myTag);
}

First of all, I recommend to place a ; after var anchors = new Array(). Second, anchors may not be preceded by var.

I think that should do it, don't know about the HTMLCollection... :yes:

Link to comment
Share on other sites

  • 0

Mattike: Thankyou, I'll try to remember those pointers. :)

Mrbester: Have my babies? :D If you get a minute or two, do you think you could explain the follow lines to me as I don't understand them.

I don't get what's happening after the "...'img','p']" part. I see that we've created an array called myTags with a few values, but after that? No idea.

var myTags=['a','abbr','img','p'],tag,tags,anchors=[],i;

The rest I could probably figure out, but I need to know whats going on there. Thanks again. :)

Link to comment
Share on other sites

  • 0

Alrighty then:

Line 1: declare the myTags array and other variables. Fairly obvious.

Line 2: start a while loop that sets the variable tag to be the last element in myTags. array.pop() returns the last element in an array and removes it from the array, so the double bracket evaluates an assignment that returns true: if there are no more elements in myTags it returns false and the while condition fails (end of loop);

first iteration: tag='p', myTags becomes ['a','abbr','img']

second iteration: tag = 'img', myTags becomes ['a','abbr']

etc.

Line 3: set the tags variable to all elements whose .tagName property matches the current value of tag.

Line 4: check if tags actually contains anything (HTMLCollection).

Line 5: set i to length of HTMLCollection referenced by tags.

Line 6: start a while loop that post-decrements i. When i==0 the loop terminates.

Line 7: add the element of the tags HTMLCollection referenced by position i to the anchors array.

This uses unrolled loops rather than the for(var x=0;x<something.length;x++) because it's a) faster and b) slinkier ;)

Mattike: You end up with an array all right, but the elements are HTMLCollections, which might confuse when enumerating later.

Link to comment
Share on other sites

  • 0

My solution:

	&lt;script type="text/javascript" &gt;
		function capture()
		{
			var i = 0;
			var Tags = new Array();
			var Anchors = new Array();

			Tags[0] = 'a';
			Tags[1] = 'abbr';

			for (i = 0; i &lt; Tags.length; i++)
			{
				Anchors[i] = document.getElementsByTagName(Tags[i]);
			}
		}

		window.onload = function() { capture() };
	&lt;/script&gt;

Edited by Simon Thulbourn
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.