12.4. Writing Elements Dynamically

jQuery allows you to easily insert elements on a page. The following code appends a <p> tag to the <div> with an id of div1:

<script>$("<p>hello I am dynamically added text</p>").appendTo("#div1")</script>

You can also insert an element after the target element with the .insertAfter() method:

<script>$("<p>hello I am dynamic text</p>").insertAfter("body")</script>

Remember that adding elements dynamically works slightly differently in IE than in Firefox. While researching this chapter, I was playing around with the following script:

<script>$("<p>hello I am dynamic text</p>").appendTo("body")</script>

This works fine in Firefox but will give you a strange error when run in IE 7 (see Figure 12-3). Can you guess ...

Get Introducing .NET 4.0: with Visual Studio 2010 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.