Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9200573
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:47:55+00:00 2026-06-17T22:47:55+00:00

I’m currently writing a website with a friend and I need to create a

  • 0

I’m currently writing a website with a friend and I need to create a javascript loop for pulling images out of a database and populating them in xy positions on a grid.

The database we’re using is built in python and django but for now I’m trying to get it working with one loop and a test image.

This is the loop in question:

function createImages(){

            var picture = document.createElement('img');{

                for (var pic=0; pic < 100; pic++) { 
                    pic.pk = 1;
                    pic.model = 'image';
                    pic.fields.title = 'Image Test';
                    pic.fields.timestamp = '2013-01-01T00:00:00.000Z';
                    pic.fields.image = 'http://i240.photobucket.com/albums/ff301/quyenhiepkhach/CAT.jpg';
                    pic.fields.height = 30 + 'px';
                    pic.fields.width = 30 + 'px';
                    pic.fields.link = '#ImageLink';
                    pic.fields.board = 1;
                    pic.fields.posx = 100 + 'px';
                    pic.fields.posy = 50 + 'px';
                    pic.fields.owner = 1;
                    pic.fields.region = 1;

                    picture.className = 'image-tooltip';
                    picture.src = pic.fields.image;
                    picture.style.marginTop = pic.fields.posy;
                    picture.style.marginLeft = pic.fields.posx;
                    picture.style.height = pic.fields.height;
                    picture.style.width = pic.fields.width;


                    document.body.appendChild(picture);


                }

            }

};

createimages();

What I have working so far:

  • Grid that is drawn onto my index page with two sections (prime and
    standard).
  • Mouseover script that displays the xy coords and standard or prime
    gridspace. (not working in jsfiddle)

What I have broken so far:

  • Javascript loop for pulling images out of database and writing them to body of page
  • Mouseover script to display some of the image data

I’ve included everything below to make the webpage and also a jsFiddle

HTML HEAD:

    <!-- Le random script for mouseover -->
    <script type="text/javascript" src="http://code.jquery.com/jquery-git.js"></script>

<!--MOUSEOVER SCRIPT FOR GRID COORDINATES-->
<script>
        $(window).load(function(){
            var tooltip = $( '<div id="tooltip">' ).appendTo( 'body' )[0];

                $( '.coords' ).    
                each(function () {       

                    var pos = $( this ).offset(),
                        top = pos.top,           
                        left = pos.left,          
                        width = $( this ).width(),  
                        height = $( this ).height();       

                    $( this ).
                        mousemove(function ( e ) {
                        var x =  ( (e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft) - left ) .toFixed( 0 ),
                            y =  ( ( (e.clientY + document.body.scrollTop + document.documentElement.scrollTop) - top ) ) .toFixed( 0 );

                        if ( x > 20 && x < 481 && y > 20 && y < 321) {
                            $( tooltip ).text( 'prime | ' + x + ', ' + y ).css({                    
                                left: e.clientX + 20,                    
                                top: e.clientY + 10                
                            }).show();
                        }

                        else {
                            $( tooltip ).text( 'standard | ' + x + ', ' + y ).css({                    
                                left: e.clientX + 20,                    
                                top: e.clientY + 10                
                            }).show();

                        }

                    }).         

                    mouseleave(function () {                
                        $( tooltip ).hide();            
                    });    

                });    

        });

</script>

<!--MOUSEOVER SCRIPT FOR IMAGES-->
<script> 
                $(window).load(function(){
            var imagetooltip = $( '<div id="imagetooltip">' ).appendTo( 'body' )[0];

            $( '.image-tooltip' ).    
                each(function () {      

                    $( imagetooltip ).text( pic.fields.title + ' , ' + pic.fields.link ).css({                    
                        left: e.clientX + 20,                    
                        top: e.clientY + 10                
                    }).show();




                    mouseleave(function () {                
                        $( tooltip ).hide();            
                    });     
                });    
        });

</script>

CSS:

        /* Style for standard section on grid */

        .grid {
            margin: 0px auto auto;
            border: 1px solid #000;
            border-width: 0 1px 1px 0;
            background-color: #28ACF9;
        }

        /* Style for grid div */

        .grid div {
            border: 1px solid #000;
            border-width: 1px 0 0 1px;
            float: left;
        }

        /* Style for prime section on grid */

        .gridprime {
            margin-top: 50px ;
            margin-left: 50px;
            border: 1px solid #000;
            background-color: #FFFF33;
            float: left;
        }

        /* Style for grid coords tooltip */

        #tooltip { 
            text-align:center; 
            background:black; 
            color:white; 
            padding:3px 0; 
            width:150px; 
            position:fixed; 
            display:none; 
            white-space:nowrap;
            z-index:3; 
        }


        /* Style for image tooltip */

        #imagetooltip { 
            text-align:left; 
            background:#CCC; 
            color:white; 
            padding:3px 0; 
            width:200px; 
            position:fixed; 
            display:none; 
            white-space:nowrap;
            z-index:4; 
        }

HTML BODY:

<!--SCRIPT TO CREATE THE GRID (WORKING)-->
<script type="text/javascript">

function creategrid(size){

            var primeW = Math.floor((460) / size),
                primeH = Math.floor((300) / size),
                standardW = Math.floor((500) / size),
                standardH = Math.floor((500) / size);

            var standard = document.createElement('div');
                standard.className = 'grid coords';
                standard.style.width = (standardW * size) + 'px';
                standard.style.height = (standardH * size) + 'px';
                standard.board = '1';

            var prime = document.createElement('div');
                prime.className = 'gridprime';
                prime.style.width = (primeW * size) + 'px';
                prime.style.height = (primeH * size)+ 'px';
                prime.style.position = 'absolute'
                prime.style.zIndex= '1';
                standard.appendChild(prime);

            for (var i = 0; i < standardH; i++) {

                for (var p = 0; p < standardW; p++) {

                    var cell = document.createElement('div');
                        cell.style.height = (size - 1) + 'px';
                        cell.style.width = (size - 1) + 'px';
                        cell.style.position = 'relative'
                        cell.style.zIndex= '2';
                        standard.appendChild(cell);

            }

        }

        document.body.appendChild(standard);

    }

    creategrid(10);

</script>



<!--SCRIPT TO LOOP IMAGES OUT OF DATABASE (USING 1 TO TEST FOR NOW)-->

<script type="text/javascript">

function createImages(){

            var picture = document.createElement('img');{

                for (var pic=0; pic < 100; pic++) { 
                    pic.pk = 1;
                    pic.model = 'image';
                    pic.fields.title = 'Image Test';
                    pic.fields.timestamp = '2013-01-01T00:00:00.000Z';
                    pic.fields.image = 'http://i240.photobucket.com/albums/ff301/quyenhiepkhach/CAT.jpg';
                    pic.fields.height = 30 + 'px';
                    pic.fields.width = 30 + 'px';
                    pic.fields.link = '#ImageLink';
                    pic.fields.board = 1;
                    pic.fields.posx = 100 + 'px';
                    pic.fields.posy = 50 + 'px';
                    pic.fields.owner = 1;
                    pic.fields.region = 1;

                    picture.className = 'image-tooltip';
                    picture.src = pic.fields.image;
                    picture.style.marginTop = pic.fields.posy;
                    picture.style.marginLeft = pic.fields.posx;
                    picture.style.height = pic.fields.height;
                    picture.style.width = pic.fields.width;

                    if (pic.fields.board = document.body.id);{
                        document.body.appendChild(picture);
                    }

                }

            }

        };

        createimages();

</script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:47:56+00:00Added an answer on June 17, 2026 at 10:47 pm

    Your code is riddled with syntax errors and logic issues. STart by using a browser console to look at errors and fix accordingly.

    Also note javascript is case sensitive so if you create a function createImages() you need to use same case to call function. You are calling createimages() which doesn’t exist

    You can’t use pic as variable to create an object in a for loop where pic is the counter.

    ALso need to create the new image within the loop, not outside it.

    Working code:

    //SCRIPT TO LOOP IMAGES OUT OF DATABASE (USING 1 TO TEST FOR NOW)//
    function createImages() {
    
        for (var pic = 0; pic < 100; pic++) {
             /* new image for each pass of loop*/
            var picture = document.createElement('img');
            var data = {
                pk: 1,
                model: 'image',
                fields: {
                    title: 'Image Test',
                    timestamp: '2013-01-01T00:00:00.000Z',
                    image: 'http://i240.photobucket.com/albums/ff301/quyenhiepkhach/CAT.jpg',
                    height: 30 + 'px',
                    width: 30 + 'px',
                    link: '#ImageLink',
                    board: 1,
                    posx: 100 + 'px',
                    posy: 50 + 'px',
                    owner: 1,
                    region: 1
                }
            };
    
            picture.className = 'image-tooltip';
            picture.src = data.fields.image;
            picture.style.marginTop = data.fields.posy;
            picture.style.marginLeft = data.fields.posx;
            picture.style.height = data.fields.height;
            picture.style.width = data.fields.width;
           /* comment out "if" since isn't true*/
           // if (data.fields.board ==document.body.id) {
                document.body.appendChild(picture);
           // }
    
        }
    
    }
    
    
    createImages();
    

    DEMO: http://jsfiddle.net/8eYhK/9/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am using JSon response to parse title,date content and thumbnail images and place
I have a small JavaScript validation script that validates inputs based on Regex. I
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.