...

View Full Version : Resolved Getting IE7 to dynamically create an image



gerald
09-17-2011, 10:33 AM
Hi,

I have the following function to dynamically create an image (in this case a toolbox button):



function createButton(srcid,bttn)
{
if (bttn=='mag') {s='Magnify';h='Magnify';o='DoZoom(event,\'0\',\''+srcid+'\')';}
if (bttn=='frz') {s='Pause';h='Freeze/Unfreeze';o='setPicture(\''+srcid+'\',0)';}
if (bttn=='swp') {s='Swap';h='Swap maps';o='setPicture(\''+srcid+'\',2)';}
btn=document.createElement('img');
btn.setAttribute('src','toolbox/'+s+'.gif');
btn.setAttribute('width','16px');
btn.setAttribute('height','16px');
btn.setAttribute('id',bttn+'btn_'+srcid);
btn.setAttribute('class','button');
btn.setAttribute('name','Toolbox');
btn.setAttribute('title',h);
btn.setAttribute('onclick',o);
document.getElementById('ctrlDiv').appendChild(btn);
}


which works fine in IE8 and IE9, but in IE7 it refuses to pick up the style 'button'. I've even tried putting in btn.class='button' but JS throws up an error.
If I specify the style by btn.style.position='Absolute' it picks this up OK. However, it won't respond to the onclick event either.
I have heard that setAttribute is buggy with IE7, which is why I tried to specify each attribute directly.

The style sheet for class button is:


.button
{
position: absolute;
left: 5;
top: 5;
z-index: 1;
}


(BTW, no link to a website as this is local on my machine atm)

TIA,

Gerald.

vwphillips
09-17-2011, 10:54 AM
btn.className='button';

gerald
09-17-2011, 12:58 PM
Excellent - works a treat. Ta.

Now just need to get the image to respond to an onclick event - apparently IE7 and earlier this is not supported and needs an <a href....> around it.
However, that doesn't explain why I have other images (not dynamically created) with an onclick event that do work on the same page...

Cheers,

Gerald.

gerald
09-17-2011, 01:27 PM
Just changed the btn.setAttribute('onclick',o) to btn.onclick=function(){runcommand}; and this works.

Regards,

Gerald.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum