Ma9ic
Ma9ic

Reputation: 1125

Add a image with java script to quiz

im using http://www.dhtmlgoodies.com/index.html?whichScript=quiz-maker but i want to be able to display a picture for each question.

ive already tried adding the likes of this to each section but it doesn't seem to work.

        (new Image()).src = "http:/track.me/image.gif"; 

and

        var img = new Image(1,1); // width, height values are optional params 
        img.src = 'http://www.testtrackinglink.com';

Here is the question var below.

        var questions = [
                {
                    label : 'Name Arsenals Football Ground?',
                    options : ['Emirates Stadium', 'Old Trafford', 'Stamford Bridge'],
                    answer : ['Emirates Stadium'],
                    forceAnswer : true,


                },
                {
                    label : 'Who was the champion of this years soccer world cup in South Africa ?',
                    options : ['Brazil', 'Netherlands', 'Spain'],
                    answer : ['Spain'],
                    forceAnswer : true  
                },
                {
                    label : 'Name two former Arsenal players ?',
                    options : ['Thierry Henry', 'Tony Adams', 'Michael Owen', 'Ole Gunnar Solskjaer'],
                    answer : [0,1], // refers to the second and third option
                    forceAnswer : true
                }
                ,
                {
                    label : 'United States has how many states',
                    options : ['49','50','51'],
                    answer : ['50'],
                    forceAnswer : true
                },
                {
                    label : 'A crocodile is a member of which family ?',
                    options : ['amphibian','reptile', 'vermin'],
                    answer : ['reptile'],
                    forceAnswer : true
                },
                {
                    label: 'In which year did Atlanta(US) arrange the summer olympics ?',
                    options : ['1992','1996','2000'],
                    answer :['1996'],
                    forceAnswer : true
                }

            ]

Upvotes: 0

Views: 2156

Answers (2)

Jitendra Pancholi
Jitendra Pancholi

Reputation: 7562

try this and let me know.

With Jquery

$.each(questions, function(i){
   var img = new Image();
   img.src='http://www.example.com/images/img.jpg';
   document.body.appendChild(img);
});

Without Jquery

for(var i=0;i<questions.length;i++){
   var img = new Image();
   img.src='http://www.example.com/images/img.jpg';
   document.body.appendChild(img);
});

Upvotes: 0

Tim S.
Tim S.

Reputation: 13843

Creating a new image does not mean it get's appended to the DOM as well. You have to add it manually.

var img = new Image();
img.src = 'http://www.example.com/images/img.jpg';

document.body.appendChild(img);

Also, make sure the element you want to append the image to exists before running the javascript.

Upvotes: 1

Related Questions