Solved

document.createElement missing self-closing slash

Posted on 2010-08-25
10
1,487 Views
Last Modified: 2013-11-19
I notice that the code below generates IMG tags that have no self-closing slash. I think this may be keeping jQuery Thickbox from working correctly on my page images.

Anyone know how to force the / ?
var image = document.createElement("img");
  image.src = "images/events/shows/" + imagePath + "thumbs/" + imageNum + "_tn.jpg";
hyperlink.appendChild(image);

Open in new window

0
Comment
Question by:sailing_12
  • 4
  • 2
  • 2
  • +1
10 Comments
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 250 total points
ID: 33528106
if you've jQuery inside :


var src = "images/events/shows/" + imagePath + "thumbs/" + imageNum + "_tn.jpg";
$("#hyperlinkID").append("<image src='" + src + "' />"); // where hyperlinkID is the ID of the hyperlink (anchor)

Open in new window

0
 
LVL 40

Expert Comment

by:gurvinder372
ID: 33528128
I think the self closing slash for IMG tag is not required when you use document.createElement
0
 
LVL 58

Expert Comment

by:cyberkiwi
ID: 33528204
Re the first post, the tag name is img not image.

var imagesrc = "images/events/shows/" + imagePath + "thumbs/" + imageNum + "_tn.jpg";
$(hyperlink).append("<img src='" + imagesrc + "' />");

However, as far as I can tell, this should have no effect on whatever is causing Thickbox not to work.
In html (not xhtml), an img without self-closing tag is correct.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:sailing_12
ID: 33531405
Yes, the self-closing slash is not necessarily required by html standards, but it's the only thing I'm seeing different in my context between a hyperlinked image that works with thickbox, and one that doesn't. So I'm thinking that maybe jQuery/thickbox needs it to be there regardless of what standard we're coding to.

I haven't actually tested this theory with static markup code - I will do that tonight and report my findings. I guess I'd prefer not to have to include additional jQuery code just to add a slash, if it can be helped - seems to me the document.createElement ought to do this.
0
 
LVL 58

Assisted Solution

by:cyberkiwi
cyberkiwi earned 250 total points
ID: 33537068
Reference:

http://stackoverflow.com/questions/348736/xhtml-is-writing-self-closing-tags-for-elements-not-traditionally-empty-bad-pra

Self-closing tags are not really valid HTML per the HTML 4 spec.  XHTML yes but not HTML.  See this quote from the link

[in IE, having elements such as <span /> in your mark-up will cause all kinds of problems with DOM traversal methods in JavaScript.]

Now go back a bit and read Alan Storm's wonderful mini-article about HTML vs XHTML and where the web is going (HTML5 vs XHTML2).

As far as I recall (and this is without testing this statement - I stand to be corrected), document.createElement works on the DOM as HTML so it produces whatever is HTML-valid.  This includes no closing tags for IMG, INPUT etc.  Like I said, I haven't tested with IE8/9 nor the very latest Fx/Chrome/Safari, so I would encourage you to do some testing and verify for yourself that this is the case.

Let us know if using static html (with closing tag) fixes your Thickbox issue.
0
 

Author Comment

by:sailing_12
ID: 33541497
Static HTML works with thick box with or without the closing tag so that is apparently not the issue.

It seems that thickbox is having issue with the dynamically generated HTML.
0
 

Accepted Solution

by:
sailing_12 earned 0 total points
ID: 33611883
0
 

Author Comment

by:sailing_12
ID: 33611931
I found my own solution to the issue, but would like to award points to those that made helpful suggestions along the way. I did not want to use the 'accept multiple solutions' option as this would incorrectly log the wrong actual solution into my knowledgebase.

Thanks.
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33674968
Thanks for the points!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question