Karthik Bammidi
Karthik Bammidi

Reputation: 1851

How to render html content within label tag using jquery

I am working on asp.net mvc. I have a javascript variable like,

var note="<strong><em><span style='color:#ed1c24;'>Hai</span></em><span style='color:#ed1c24;'>Welcome</span></strong> ";

I need to display as a html with in the label so i tried it like,

$('#lblNote').html(note);

but it doesnt render the html content and it shows the html tags as it is like,

<label id="lblNote"><strong><em><span style=""color:#ed1c24;"">
    Hai</span></em><span style=""color:#ed1c24;"">
    Welcome</span></strong> </label>

please guide me.

Upvotes: 0

Views: 1600

Answers (4)

Ram
Ram

Reputation: 144669

There are two syntax errors in your code, 1. change the wrapping quotes to single quotes or escape them 2. and remove the line-breaks or concatenate the strings.

var note = '<strong><em><span style="color:#ed1c24;">Hai</span></em><span style="color:#ed1c24;">Welcome</span></strong>';

http://jsfiddle.net/VsGqg/


You should replace the characters:

$('#lblNote').html(note.replace(/&lt;/g, '<').replace(/&gt;/g, '>'))

Upvotes: 2

jose
jose

Reputation: 2543

Format your string properly. note should be

var note="<strong><em><span style=\"color:#ed1c24;\">
Hai</span></em><span style=\"color:#ed1c24;\">
Welcome</span></strong> ";

Upvotes: 1

Hkachhia
Hkachhia

Reputation: 4539

Try this

var note="<strong><em><span style='color:#ed1c24;'>Hai</span></em><span style='color:#ed1c24;'>
Welcome</span></strong> ";

$('#lblNote').html(note);

Upvotes: 1

silly
silly

Reputation: 7887

escape your double quotes like this

var note="<strong><em><span style=\"color:#ed1c24;\">
Hai</span></em><span style=\"color:#ed1c24;\">
Welcome</span></strong> ";

Upvotes: 1

Related Questions