JKumar
JKumar

Reputation: 1

Adding a span tag between the text of anchor tag using jquery

How would you add a span tag before the text inside a hyperlink? Right now, I'm using this:

<script type="text/javascript">
    $("a").before('<span class="k-icon k-add"></span>');
</script>

I tried the above code but the required span is coming before the anchor tag

<span class="k-icon k-add"></span><a href="some page">Add</a>

I need the link to look like this when it is parsed:

<a href="some page"><span class="k-icon k-add"></span>Add</a>

Any ideas?

Upvotes: 0

Views: 3226

Answers (2)

ant_Ti
ant_Ti

Reputation: 2425

I suggest you to add icons not with js but with pseudo elements in css

It will be something like this:

a.k-icon.k-add:before {
    width: 10px;
    height: 10px;
    background: #ccc;
    content: '';
}

http://jsfiddle.net/frsxy8vb/

Upvotes: 0

Denys S&#233;guret
Denys S&#233;guret

Reputation: 382150

Use prepend to add some content at the start of your element :

$("a").prepend('<span class="k-icon k-add"></span>');

Upvotes: 3

Related Questions