If I understand your problem well, what you want is to escape the HTML tags so you can see the source code like this:
The world!
.
For this you have to transform some of the symbols of this HTML string into their corresponding HTML entities.
I suggest using a library for this eg he.js
The code looks like this:
$(function(){
var val = $(".val-input").html(); // aqui tem de ser .html()
var text = he.escape(val); // <- esta é a linha que queres
$("#mostrador").html(text); // aqui tem de ser .html()
});
example: link
Option for simple strings:
If the strings are simple you can use this code :
var entityMap = {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
};
function escapeHtml(string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return entityMap[s];
});
}
What the code does is look in the string for characters like &<>"'\/
and substitute them in the string for its visual representation in HTML, which is in the entityMap
object.
Example: link