Saswat
Saswat

Reputation: 12806

Append html within anchor tag using jquery(not javascript)

I have a html code

<a href="javascipt:void(0)" class="chosen-single" tabindex="-1">
<span>Thutmekri</span>
</a>

Which i want to change into

<a href="javascipt:void(0)" class="chosen-single" tabindex="-1">
<span>1 - Test Setup123</span><div><b></b></div>
</a>

How can I achieve this using jquery(not javascript)?

$(".chosen-single").text() changes text under anchor tag, but how can i change it having some html as well??

in javascript we have innerHTML.

What is equivalent in jquery?

Upvotes: 0

Views: 1665

Answers (4)

Mackan
Mackan

Reputation: 6271

For the inner text in span, you can use jQuery html():

$(".chosen-single span").html('1 - Test Setup123');

Then you can append the div after the span, by using jquery insertAfter():

$('<div><b></b></div>').insertAfter('.chosen-single span');

Upvotes: 1

Mattis
Mattis

Reputation: 5096

Or just do it like this, gives you more control:

$('.chosen-single').find('span').prepend('<div><b></b></div>');

Name the spans with a class or id to make it more specific.

Upvotes: 0

dsharew
dsharew

Reputation: 10665

How about this?

$(".chosen-single").html(yourHtml)

Upvotes: 2

Milind Anantwar
Milind Anantwar

Reputation: 82231

You need to use .html()/.html(newhtml) for getting/setting html content:

 $(".chosen-single").html('<span>1 - Test Setup123</span><div><b></b></div>');

Upvotes: 3

Related Questions