Dev P
Dev P

Reputation: 1187

Adding a <img> to an div tag

I have the following structure:

<div class="eventIcons">Sample Event</div>

How do I add an tag to the div before the text so that I end up with:

<div class"eventIcons"><img id="printOnlyIcon" src"printOnly.jpg"/>Sample Events</div>

I am currently using the following jquery but its adding the tag after the text:

$('div.eventIcons').append('<img id="printOnlyIcon" src="printOnly.jpg"/>');

Many Thanks

Upvotes: 2

Views: 128

Answers (3)

GautamD31
GautamD31

Reputation: 28763

Try like this

$('div.eventIcons').prepend('<img id="printOnlyIcon" src="printOnly.jpg"/>');

You have to prepend the imag to your div inorder to append it.

Upvotes: 0

Lazar Vuckovic
Lazar Vuckovic

Reputation: 808

Just use "prepend" instead of "append".

Upvotes: 1

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79830

Use .prepend to add element at the beginning.

See below,

$('div.eventIcons').prepend('<img id="printOnlyIcon" src="printOnly.jpg"/>');

Upvotes: 9

Related Questions