2

I have stored a c++ code snipet in javascript variable like this:-

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="Assets/lib/jquery-1.11.1.min.js"></script>
        <title>JSP Page</title>
        <script>
                        var s = "#include<iostream.h>#include<conio.h>";
                            $(document).ready(function(){
                            $("#btn").click(function(){
                                document.getElementById("text").innerHTML = s;
                            });
                        });
        </script>
    </head>
    <body>
        <button id="btn">show</button>
        <h1><p id="text"></p></h1>
    </body>
</html>

on the JSP page when I click the button it prints only "#include#include". It is not printing the whole thing as i assign to the variable s.

So, my question is, How I can print the things between the "<>" angle brackets.

MaxSteel
  • 513
  • 1
  • 4
  • 11

3 Answers3

2

You must use HTML entities to represent these characters.

Replace "<" with &lt;, and ">" with &gt;

Here is a complete list of entities: Character Entity Reference Chart

0

on the JSP page when I click the button it prints only "#include#include". It is not printing the whole thing as i assign to the variable s.

Because , jquery interprets the <> tags inside it .

How I can print the things between the "<>" angle brackets.

To print the elements inside the tags as it is , you need to escape them either as &lt; or &LT; Similarly for the other one as &gt; or &GT;

Have a look at printing "<html>" using html.

Community
  • 1
  • 1
Santhosh
  • 8,181
  • 4
  • 29
  • 56
0

Check this demo, you should use escape character for showing html to browser like &lt; for < , &gt; for > and so on. or you can also try text() function of ajax. it will escape your text as per require.

Like this:

 $("#text1").text(s1);

Check this snippet.

var s = "#include&lt;iostream.h&gt;#include&lt;conio.h&gt;";
var s1 = "#include<iostream.h>#include<conio.h>";
$(document).ready(function(){
 
  $("#btn").click(function(){
    document.getElementById("text").innerHTML = s;
    $("#text1").text(s1);
    
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script src="Assets/lib/jquery-1.11.1.min.js"></script>
        <title>JSP Page</title>
        
    </head>
    <body>
        <button id="btn">show</button>
        <h1><p id="text"></p></h1>
      <h1><p id="text1"></p></h1>
    </body>
</html>
bharatpatel
  • 1,203
  • 11
  • 22