...

View Full Version : [Javascript] - add div contents to a array



conware
09-05-2011, 05:32 PM
Hi guys I was wondering if its possible to add contents within a div to a javascript array. Something like this:



<script type="text/javascript">
function returnArray()
{
var divContent = document.getElementById('images');
var matchImage = divContent.match(/<img/);
/* add images into array somehow */
alert('This is ' +ImageArray[1]);

}
</script>

<div id="images">
<img src="image1.jpg" alt="image" />
<img src="image2.jpg" alt="image" />
<img src="image3.jpg" alt="image" />
</div>



Thanks for any info.

DaveyErwin
09-05-2011, 07:19 PM
<script type="text/javascript">
function returnArray()
{
var divContent = document.getElementById('images');
//var matchImage = divContent.match(/<img/);
/* add images into array somehow */
//alert('This is ' +ImageArray[1]);
var a = new Array();
for(var i in divContent.childNodes){
if(divContent.childNodes[i].nodeType==1){
a[i]=(divContent.childNodes[i].src);
} }
return a;

}

function init(){
var a =returnArray();
alert(a)
}

</script>

</head>
<body onload="init()">


<div id="images">
<img src="image1.jpg" alt="image" />
<img src="image2.jpg" alt="image" />
<img src="image3.jpg" alt="image" />
</div>

conware
09-06-2011, 11:18 AM
Thanks for the code DaveyErwin, now I get how it works :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum