Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using jQuery to echo text

Is it possible to use jQuery to echo text in place of a script tag? More precisely, is there a way to accomplish

<script type="text/javascript">
    document.write("foo");
</script>

... without the use of document.write? I am not happy about using document.write after reading this.

I am aware that I could alternatively do this:

<span id="container"></span>
<script type="text/javascript">
    $("#container").text("foo");
</script>

However, I'm interested to see if there's a way to do it without using a container element, preferably using jQuery.

Thanks in advance!

like image 632
susmits Avatar asked Apr 01 '10 17:04

susmits


1 Answers

If you come up with a jQuery way of doing document.write(), it'll be bad for all the same reasons.

You are better off just using document.write() if that's what you need, or better yet, manipulating an existing element or appending a new element somewhere in the DOM--that's what jQuery is good at.

like image 67
Michael Haren Avatar answered Sep 30 '22 01:09

Michael Haren