Solved

jQuery: Image closing tag destroyed

Posted on 2012-04-01
8
314 Views
Last Modified: 2012-06-27
I use jQuery to append an image tag.  The problem is that the closing tag for the image is destroyed!

<img /> becomes <img>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Demo</title>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function() {
 $('div').append('<img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/Peach_flowers.jpg/287px-Peach_flowers.jpg" alt="" />');
 alert( $('div').html() );
});


</script>
</head>
<body>

<div></div>

</body>
</html>

Open in new window

0
Comment
Question by:hankknight
8 Comments
 
LVL 17

Assisted Solution

by:selvol
selvol earned 125 total points
Comment Utility
In HTML the <img> tag has no end tag.

will that still work for you?

Why do you need to close the img?
0
 
LVL 82

Assisted Solution

by:leakim971
leakim971 earned 125 total points
Comment Utility
0
 
LVL 16

Author Comment

by:hankknight
Comment Utility
The document type in my example is XHTML 1.0.

I use this script to create VALID XHTML that users can copy and paste onto websites.

How can I get those closing tags in place?
0
 
LVL 17

Expert Comment

by:selvol
Comment Utility
Change this line to point to the attached js.

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
Change to

<script type="text/javascript" src="g.js"></script>


Selvol
g.js
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 17

Expert Comment

by:selvol
Comment Utility
Your end result is this.

img tag
To obtain that result I edited the ......min.js by removing the "img" parsing function for that tag.


Selvol
0
 
LVL 42

Assisted Solution

by:David S.
David S. earned 125 total points
Comment Utility
@Hank  What are you trying to achieve here?  Is there a particular reason that you're appending the <img> to a <div> and then displaying the <div>'s contents? Why the intermediate step?

@Selvol  What happens if Hank is using some other code that's relying on jQuery to not have that modification?
0
 
LVL 17

Expert Comment

by:selvol
Comment Utility
@Kravimir  "What happens if Hank is using some other code that's relying on jQuery to not have that modification?"
Should probally be a second question. As you know.
0
 
LVL 20

Accepted Solution

by:
Proculopsis earned 125 total points
Comment Utility
If you insist:

    $('div').html(

    $("<img />").attr({
        src: "http://upload.wikimedia.org/wikipedia/commons/thumb/f/f2/Peach_flowers.jpg/287px-Peach_flowers.jpg",
        alt: ""
    }).get(0).outerHTML + "</img>"

    );

    alert($('div').html());

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now