Inserting Content Before, After, and Inside Elements with jQuery

jQuery provides you with easy ways to get at the data in your Web page. In particular, you can insert any content you want, anywhere on your page. The examples that follow all use this sample code:


<html>
<head>
<title>My Test Page</title>
<script type="text/javascript" src="js/jquery-1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){

//Your code goes here.

});
</script>
</head>
<body>
<div>I am a div.</div>
<br />
<input value="Go" type="submit">
</body>
</html>

Here's a quick rundown on how to insert content before, after, and inside a <div> element in a Web page:























To Place ContentCode
In front of and outside the class="code"><div> (before)$(":submit").click(function () {class="code">

class="code">$("div").before("Before<br />");class="code">

});
In back of and outside the class="code"><div> (after)$(":submit").click(function () {class="code">

class="code">$("div").after("After<br />");class="code">

});
Inside the <div> but before
existing content (prepend)
$(":submit").click(function () {class="code">

class="code">$("div").prepend("New Content!");class="code">

});
Inside the <div> but after
existing content (append)
$(":submit").click(function () {class="code">

class="code">$("div").append("New Content!");class="code">

});



dummies

Source:http://www.dummies.com/how-to/content/inserting-content-before-after-and-inside-elements.html

No comments:

Post a Comment