0

I have a string being sent to my JavaScript function from my C# code which has a text like:

 Hi <b>Welcome to C#<sup>5.0</sup></b>

I'm populating a div with this string. I need this html content to be rendered in the screen with the bold and superscript applied as below

Hi Welcome to C#5.0

I tried the below it just displays the HTML tags as plain text

myfunction = function(data) {
  var mytext = '<div>'+ data +'</div>;
  $('#mydivid').after(mytext);
}

Is there any other solution neither like the one given here nor like this ?

Note: I cannot declare the string in a hidden variable in my .cshtml file within @HTML.Raw and again use it in my JavaScript.

I have tried using $.parseHTML but I got an error saying

$.parseHTML is not a function

Any solution other than the above solutions are welcome. Thanks in advance.

7
  • 4
    You've tagged this question as jquery, so why aren't you using the html() function? Commented Dec 21, 2016 at 11:54
  • 1
    and more importantly, what have you tried so far? Commented Dec 21, 2016 at 11:55
  • you can directly replace this line var mytext = '<div>'+ data +'</div>; with razor syntax var mytext = '<div>'+ '@Html.Raw(data)' +'</div>; ... As you said I cannot declare the string in a hidden variable .. so with this syntax there is no need of variable Commented Dec 21, 2016 at 12:03
  • @RajshekarReddy are you telling me include Html.Raw in my .js file. Is that possible? No right? Commented Dec 21, 2016 at 12:08
  • 1
    @SethuBala but still there is a plugin which enables the power to use Razor syntax in .js file nuget.org/packages/RazorJS Commented Dec 21, 2016 at 12:10

3 Answers 3

3

seems like nothing is wrong with your code. Check if some css is overriding the basic em and sup functionality.

for Example the default style of em is font-style: italic which chould be override by your css

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah! You were right. I had to override my css. Thank you :)
2

Try like this

myfunction = function(data) {
  $('#mydivid').html('<div>' + data + '</div>');
}

3 Comments

This solution will work well for replacing .append() (sorry that was a late edit in my question) with .html() but in my case I'm using .after(). So what should I do for this case.
try like this $('#mydivid').after('<div>' + data + '</div>');
Tried but no improvement
1

You listed JQuery in your tags so I figured this would help.

html = $.parseHTML(  'Hi <b>Welcome to C#<sup>5.0</sup></b>' );

Check out this JSFiddle

You can also do this if you want a javascript oneliner.

document.getElementById("mydivid").innerHTML="Hi <b>Welcome to C#<sup>5.0</sup></b>";

8 Comments

Tried this solution already and got error as "$.parseHTML is not a function"
Are you sure you have Jquery included in your scripts?
Yes. I'm pretty sure about that :)
What version are you running?
jQuery version "1.7.1"
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.