Solved

jQuery: Image closing tag destroyed

Posted on 2012-04-01
8
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 17

Assisted Solution

by:selvol
selvol earned 125 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 82

Assisted Solution

by:leakim971
leakim971 earned 125 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
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!

 
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 42

Assisted Solution

by:David S.
David S. earned 125 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 125 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

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…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

726 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