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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Catherine Lynn Smith

    #1

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

    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
  • Martin Honnen

    #2
    Re: createElement(' img') and .width properties in percentages?



    Catherine Lynn Smith wrote:
    [color=blue]
    > 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?[/color]

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

    var img = document.create Element('img');
    img.src = 'kiboInside.gif ';
    img.setAttribut e('width', '100%');
    img.setAttribut e('height', '100%');
    img.alt = 'Kibology';
    document.body.a ppendChild(img) ;

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

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

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: createElement(' img') and .width properties in percentages?

      klynntg@hotmail .com (Catherine Lynn Smith) writes:
      [color=blue]
      > But if I try to create it within the javascript, it will not work.
      >
      > imgObj = createElement(' img');
      > imgObj.width = "100%";[/color]
      [color=blue]
      > Is there a way to do this?[/color]

      Use styles
      imgObj.style.wi dth="100%";

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

      Comment

      Working...