Solved

jQuery: Image closing tag destroyed

Posted on 2012-04-01
8
322 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
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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
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

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Jquery get each value 2 31
How to convert from xls to xlsx using java 7 39
Checking data with javascript 3 19
Mobile page Scrolling down when refreshing 4 30
When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
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…

770 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