Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2010
    Posts
    199
    Thanks
    78
    Thanked 5 Times in 5 Posts

    [Javascript] - add div contents to a array

    Hi guys I was wondering if its possible to add contents within a div to a javascript array. Something like this:
    Code:
    <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.

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Code:
    <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>

  • Users who have thanked DaveyErwin for this post:

    conware (09-06-2011)

  • #3
    Regular Coder
    Join Date
    Mar 2010
    Posts
    199
    Thanks
    78
    Thanked 5 Times in 5 Posts
    Thanks for the code DaveyErwin, now I get how it works


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •