createElement('img') and .width properties in percentages?

  • Thread starter Catherine Lynn Smith
  • Start date
C

Catherine Lynn Smith

I want to use javascript to build page elements using onLoad but I
seem to be having trouble defining the width value of an IMG tag if I
create it with javascript.

If I build the element with HTML it works just fine with:

<img src="foo.gif" width="100%" height="100">

But if I try to create it within the javascript, it will not work.

imgObj = createElement('img');
imgObj.width = "100%";

Is there a way to do this?

KL
 
M

Martin Honnen

Catherine said:
I want to use javascript to build page elements using onLoad but I
seem to be having trouble defining the width value of an IMG tag if I
create it with javascript.

If I build the element with HTML it works just fine with:

<img src="foo.gif" width="100%" height="100">

But if I try to create it within the javascript, it will not work.

imgObj = createElement('img');
imgObj.width = "100%";

Is there a way to do this?

Yes, with Netscape 6/7, Mozilla, and Opera 7 you can use

var img = document.createElement('img');
img.src = 'kiboInside.gif';
img.setAttribute('width', '100%');
img.setAttribute('height', '100%');
img.alt = 'Kibology';
document.body.appendChild(img);

however that way IE/Win will insert the img in its normal size. I guess
for IE you need to use insertAdjacentHTML instead of W3C DOM methods
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top