+ Reply to Thread
Results 1 to 2 of 2

Thread: Javscript: Creating a self-closing img tag with createElement?

  1. #1
    Tarzan is offline x10Hosting Member Tarzan is an unknown quantity at this point
    Join Date
    Jun 2008
    Location
    Sweden
    Posts
    65

    Javscript: Creating a self-closing img tag with createElement?

    Hi!
    I'm trying to create a img tag dynamically with javascript. So far I've used the createElement function.
    The problem is however that the created tag seems to be open:
    Code:
    <img src="target">
    Is there a way to create a self-closing img tag?
    Code:
    <img src="target" />
    I would prefer not to use .innerHTML since I want the img to become part of the DOM tree (and looks cleaner )

  2. #2
    essellar's Avatar
    essellar is offline Community Advocate essellar has a spectacular aura about
    Join Date
    Feb 2010
    Location
    Toronto, Ontario, CA
    Posts
    1,153

    Re: Javscript: Creating a self-closing img tag with createElement?

    If you are working against an HTML (as opposed to XHTML) DOM, there is no need to explicitly close a unary tag (like <img>, <hr> or <br>). The HTML does not appear in the source in any case; it only exists in the modified DOM (which may be accessible using something like View Rendered Source, the developers' toolkit or a debugger). Since what you are seeing is not source code but an HTML rendering of the DOM as it exists in memory, the viewer will render whatever the valid representation of that element happens to be. Strictly speaking, the slash at the end of a unary tag in an HTML document is an illegal garbage character, even if it is required in XHTML (which is a dialect of XML that only superficially resembles HTML).
    “Beware of bugs in the above code; I have only proved it correct, not tried it.” --Donald Knuth
    "It was as if its architects were given a perfectly good hammer and gleefully replied, 'neat! With this hammer, we can build a tool that can pound in nails.'" -- Alex Papadimoulis (on TheDailyWTF.com)

+ Reply to Thread

Similar Threads

  1. New Closing System =)
    By mattblog in forum Feedback and Suggestions
    Replies: 4
    Last Post: 06-06-2010, 04:41 PM
  2. closing account
    By mopar321 in forum Free Hosting
    Replies: 1
    Last Post: 05-12-2010, 11:42 PM
  3. Closing account
    By asrcmad in forum Free Hosting
    Replies: 1
    Last Post: 12-19-2009, 04:32 PM
  4. Closing a account
    By pvinvest in forum Free Hosting
    Replies: 2
    Last Post: 12-09-2009, 10:03 PM
  5. Account Closing
    By Awarapan in forum Free Hosting
    Replies: 1
    Last Post: 08-06-2007, 09:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers