0

I am attempting to add an image to each save button on the page.

The jQuery I am using is this:

$("div.save-to-button").append
('<img class="saveToDropDownIcon" src="../../Content/Images/downTriangleIcon.png"
alt="V" />');

The jQuery statement above produces this html:

<div class="save-to-button" 
title="Save this recipe to the shopping list or a meal plan."> Save To 
<img class="saveToDropDownIcon" src="../../Content/Images/downTriangleIcon.png"
alt="V"></div>

As you can see, the html sent to the append method closes the img tag correctly, but the resulting html does not close the img tag.

I have also tried

$("div.save-to-button").append('<img class="saveToDropDownIcon" 
src="../../Content/Images/downTriangleIcon.png" alt="V" ></img>');

Which produced the same result.

Could someone please point out what I am doing wrong.

Thanks, Scott

6
  • Um...what's wrong with the result? Commented Mar 3, 2012 at 1:11
  • Which browser and what's the doctype? jQuery interprets your .append() parameter to create DOM nodes, not markup. Commented Mar 3, 2012 at 1:12
  • aren't DOM nodes markup? Commented Mar 3, 2012 at 1:15
  • don't be mislead by what browser does in console with closing image tags...it's up to browser how they get handled. jQuery is cross browser compliant Commented Mar 3, 2012 at 1:17
  • @tq: DOM nodes are not markup. Markup is text. The markup text is interpreted to create DOM nodes, which (when in a JavaScript environment) are JavaScript host objects. Commented Mar 3, 2012 at 1:22

2 Answers 2

2

It doesn't close the <img> tag at all. It discards your HTML after converting it to DOM elements.

What you're seeing is the browser's interpretation of what the HTML would look like if it was still HTML. It gets to choose for itself how it looks, which may be entirely different from the original HTML.

Remember that in the browser there is no HTML. There's only the DOM and its API, which is capable of analyzing the DOM and generating its interpretation of an HTML string from it.


To be clear, there is no general requirement for /> in elements that do not allow content, like <img>. That's a restraint specific to XHTML. Doing <img ... > is perfectly valid, unless prohibited by your DOCTYPE.

Sign up to request clarification or add additional context in comments.

2 Comments

but why wouldn't it display the img node as closed, even if it's just an interpretation?
@joakimdahlstrom: Because the /> requirement is specific to XHTML. Elements like <img> that don't allow content do not actually need the / at all. It's just that XHTML artificially mandates it for validation. Just like in most cases quotation marks are not actually required for attribute values, but XHTML requires them. So the browser chooses to render the HTML in a non XHTML compliant manner.
0

use prepend

 $("div.save-to-button").prepend('<img class="saveToDropDownIcon" src="../../Conten/Images/downTriangleIcon.png"alt="V" />');

1 Comment

prepend is the same as append, but differs in where the dom node is placed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.