Arish
Arish

Reputation: 342

add a span tag inside h2 tag jquery

I need to add a span tag inside every h2 tags

my problem is im having an anchor tag inside h2 tag like this

<h2><a href=#">something</a></h2>

I have tried this code

$('h2').wrappInner('<span />');

It is wrapping span tage before the anchor tag like

<h2><span><a href="#">something</a></span></h2>

What i want is this:

<h2><span></span><a href="#">something</a></h2>

Is there any way to do this?

Upvotes: 2

Views: 3467

Answers (3)

A. Wolff
A. Wolff

Reputation: 74420

Try this instead:

$('h2').prepend('<span />');

Upvotes: 2

Sang Suantak
Sang Suantak

Reputation: 5265

Do this:

$('h2').prepend('<span />');

Upvotes: 2

Shadow Wizzard
Shadow Wizzard

Reputation: 66388

Use the prepend() method:

$('h2').prepend('<span />');

Live test case.

Upvotes: 5

Related Questions