?
Solved

jQuery: Image closing tag destroyed

Posted on 2012-04-01
8
Medium Priority
?
345 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 500 total points
ID: 37793268
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 83

Assisted Solution

by:leakim971
leakim971 earned 500 total points
ID: 37793274
0
 
LVL 16

Author Comment

by:hankknight
ID: 37793371
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 17

Expert Comment

by:selvol
ID: 37793939
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
 
LVL 17

Expert Comment

by:selvol
ID: 37793949
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 43

Assisted Solution

by:David S.
David S. earned 500 total points
ID: 37794047
@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
ID: 37794119
@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 500 total points
ID: 37796707
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
Suggested Courses

862 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