By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,288 Members | 1,695 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,288 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a


Catherine Lynn Smith wrote:
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
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2

P: n/a
kl*****@hotmail.com (Catherine Lynn Smith) writes:
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?


Use styles
imgObj.style.width="100%";

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.