jQuery Data Type How to - Convert type string to type Text and add to document








Question

We would like to know how to convert type string to type Text and add to document.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
window.onload=function(){<!--from  ww w. ja  va2 s  . c  o m-->
var projects = {
    description: 'blablabla description bla bla bla more description'
};
var newText = document.createElement('p');
var newContent = document.createTextNode(projects.description);
if (newContent.nodeValue.length > 40)
    newContent.nodeValue = newContent.nodeValue.substring(0, 37) + '...';
newText.appendChild(newContent);
document.body.appendChild(newText);
}
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: