Skip to content

Instantly share code, notes, and snippets.

@bennadel
Created March 25, 2014 11:14
Script Tags, jQuery, And Html(), Text() And Contents()
<!DOCTYPE HTML>
<html>
<head>
<title>jQuery: Scripts Tags And Text(), HTML(), and Contents()</title>
<script type="text/javascript" src="jquery-1.4.js"></script>
<script type="text/javascript">
// When the DOM is ready, init scripts.
jQuery(function( $ ){
// Get a reference to our Script tag.
var script = $( "#data" );
// Get a reference to our output tags.
var textOutput = $( "#text-output" );
var htmlOutput = $( "#html-output" );
var contentsTextOutput = $( "#contents-text-output" );
var contentsHtmlOutput = $( "#contents-html-output" );
// Use the various approachs to output the contents
// of the Script tag to the document.
// Text.
textOutput.text( script.text() );
// Html.
htmlOutput.text( script.html() );
// Contents - Text.
contentsTextOutput.text( script.contents().text() );
// Contents - Html.
contentsHtmlOutput.text( script.contents().html() );
});
</script>
</head>
<body>
<h1>
jQuery: Scripts Tags And Text(), HTML(), and Contents()
</h1>
<script id="data" type="application/x-json">
{script-content}
</script>
<!-- This is where the contents of script will be output. -->
<p>
TEXT: <span id="text-output"> ... </span>
</p>
<p>
HTML: <span id="html-output"> ... </span>
</p>
<p>
CONTENTS (TEXT): <span id="contents-text-output"> ... </span>
</p>
<p>
CONTENTS (HTML): <span id="contents-html-output"> ... </span>
</p>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment