pooja
pooja

Reputation: 58

How to dynamically add em tag before img?

I want to put em tag before IMG tag dynamically , the image class is .photo.

<img class="photo" src="image.jpg" />

I want to convert this to

<em></em><img class="photo" src="image.jpg" />

using jQuery, I am using the append but its add the em in the img tag,

thanks.

Upvotes: 0

Views: 859

Answers (4)

moxn
moxn

Reputation: 1800

jQuery("img").before("em")

EDIT: Okay, no need to add the closing tag.

Upvotes: 0

Matt Howell
Matt Howell

Reputation: 15946

$("img.photo").before("<em>");

Upvotes: 3

Vladislav Rastrusny
Vladislav Rastrusny

Reputation: 29985

You need to use before()

Upvotes: 1

kgiannakakis
kgiannakakis

Reputation: 104178

You can use the before() method.

Upvotes: 1

Related Questions