views:

261

answers:

4

Dear all,

What is wrong with this code? Why is my imagemap not working?

function createimg()
 {
         var img = new Image();
       img.src='link/to/image';
       img.alt='Next image';  img.id = 'span1'; img.style.zIndex = 10;
       img.style.position = 'absolute';  
       img.style.display='block'; 
       img.style.top = '130px';
       img.style.padding='10px'; 
       img.style.left='440px'; 
       img.usemap='#testmap';  
       img.className ='dynamicSpan';
       document.body.appendChild(img);

        var mymap = document.createElement('map');
        mymap.name = 'testmap';
        document.body.appendChild(mymap);

        var areatag = document.createElement('area');
        areatag.shape = 'rect';
        areatag.coords = '900,200,1100,1000' ;
        areatag.href =   'http://www.google.com';
        mymap.appendChild(areatag);
        document.body.appendChild(areatag);

        return img;
   }

UPDATE:

I reconstructed my code like this, but it is still not functional:

 function createimg()
 {
         var img = new Image();
       img.src='link/to/image';
       img.alt='Next image';
       img.id = 'span1';
       img.style.zIndex = 10;
       img.style.position = 'absolute';
       img.style.display='block';
       img.style.top = '130px';
       img.style.padding='10px';
       img.style.left='440px';
       img.usemap='#testmap';
       img.className ='dynamicSpan';


        var mymap = document.createElement('map');
        mymap.name = 'testmap';
        mymap.id = 'testmap';


        var areatag = document.createElement('area');
        areatag.shape = 'rect';
        areatag.coords = '0,0,500,500' ;
        areatag.href =   'http://www.google.com';
        areatag.target = '_blank';


        //append area to map
        mymap.appendChild(areatag);
        // append map to document
        document.body.appendChild(mymap);
        //append image to document
        document.body.appendChild(img);

        return img;
   }
+1  A: 

You have created element instance "mymap", but didn't added (not appended) it to the document, as you did it for "img" (appendChild).

document.createElement(name) creates an instance of element, but not appends it to the document.

Viktor Jevdokimov
I've done that now... but no difference. Still no imagemap on my image? Could anything else be wrong?
Tony
A: 

In addition to Viktor's answer:

img.usemap='#testmap';
...
mymap.name = 'testmap';

You will probably need to give mymap an id of "testmap"

geowa4
+1  A: 

it may be that the order in which you do things is not good.

i think you should:

  1. create img element (+set its attrs)
  2. create map element (+set its attrs)
  3. create area element (+...)
  4. append area to map
  5. append map to document
  6. append image to document
Ovaj Onaj
+1  A: 

here's the solution:

you should use

img.setAttribute("usemap", '#testmap')

instead of:

img.usemap = "#testmap"

Ovaj Onaj
Thank you so much for fixing it! It works now!! :) :) :)
Tony
just found out... it could also have been img.useMap -> but note the case. the thing is, html is not case-sensitive, but JS is. So while in html it can be usemap, USEMAP, useMap, UsEmAp, etc, in JS it must be useMap.luckily, setAttribute sets the html attr. so there the case (again) isn't important.
Ovaj Onaj