Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating array of certain elements 2

Status
Not open for further replies.

Breadcrust

Programmer
Oct 27, 2003
42
0
0
AU
Hi all,

I'm not much of a Javascript programmer, but I need to do a little for page I'm working on. Anyway, I need help with this problem.

I need to create an array of all the <div> elements that have a parent element with the class of "mr".

In other words, what I want to do is kind of like
var divs = document.getElementsByTagName('div');
except i just want to filter by the parent's class.

any help with this would be great ^^

--
breadcrust
 
Something like this should work:

Code:
var divs = document.getElementsByTagName('div');
var filteredDivs = [];
for (var loop=0; loop<divs.length; loop++) {
	if (divs[loop].parentNode.className == 'mr') {
		filteredDivs[filteredDivs.length] = divs[loop];
	}
}

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
this should work in IE just fine, not 100% sure about firefox:
Code:
<script language=javascript>

var divArr = new Array();

function findDiv() {
   var tempArr = document.getElementsByTagName("div");
   for (i = 0; i < tempArr.length; i++) {
      if (tempArr[i].parentNode.className == "mr") {
         divArr[divArr.length] = tempArr[i];
      }
   }
}

</script>

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 
wow, I was super slow on that one.....

-kaht

...looks like you don't have a job, so why don't you get out there and feed Tina.
[banghead]
 

Whitespace and text nodes cannot have children, surely?

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
thanks for that. :)
that code works on IE, Gecko and KHTML, but not Opera. :-/
is it just me, or does Opera have quite a bit of problems with Javascript?

--
breadcrust
 
o_O

okay... its working on Opera now. hmmm, thats even better, thanks again! ^_^

--
breadcrust
 
Are you waiting until the page has loaded before calling the code? Check out this test harness:

Code:
<html>
<head>
	<script type="text/javascript">
	<!--
		function findDivs() {
			var divs = document.getElementsByTagName('div');
			var filteredDivs = [];
			for (var loop=0; loop<divs.length; loop++) {
				if (divs[loop].parentNode.className == 'mr') {
					filteredDivs[filteredDivs.length] = divs[loop];
				}
			}
			alert(filteredDivs[0].id);
		}
	//-->
	</script>
</head>
<body onload="findDivs();">
	<div class="mr" id="d1">
		<div id="d2">
		</div>
	</div>
</body>
</html>

it works fine for me in IE, NN, FF, and Opera 7, alerting "d2" when the page has loaded. Do you not see that in Opera?

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
no, im running the code as the page loads. but im also running this code after the html code of the divs i searching for. a major problem for me is that I cant change the body arguments to put an onload argument in (dont ask why).

--
breadcrust
 
now that I think of it, knowing how to execute code after the page has loaded (without doing it through <body onload="">) could be really useful for other things. is this possible? if not, how I could execute code after a given amount of time?

--
breadcrust
 
Instead of this:

Code:
<html>
<body onload="someFunc();">

you can use:

Code:
<html>
<head>
   <script type="text/javascript">
   <!--
      window.onload = someFunc;
   //-->
   </script>
</head>

<body>

Hope this helps,
Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
alright, that helps alot.

thanks again for helping a Javascript noobie guys! :)

--
breadcrust
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top