Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying jQuery 2.0 Development Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
jQuery 2.0 Development Cookbook

jQuery 2.0 Development Cookbook

By : Leon Revill
4.2 (12)
close
close
jQuery 2.0 Development Cookbook

jQuery 2.0 Development Cookbook

4.2 (12)
By: Leon Revill

Overview of this book

Taking a recipe-based approach, this book presents numerous practical examples that you can use directly in your applications. The book covers the essential issues you will face while developing your web applications and gives you solutions to them. The recipes in this book are written in a manner that rapidly takes you from beginner to expert level. This book is for web developers of all skill levels. Although some knowledge of JavaScript, HTML, and CSS is required, this Cookbook will teach jQuery newcomers all the basics required to move on to the more complex examples of this book, which will benefit the more seasoned jQuery developer. If you want to learn how to create modern website features quickly, using best practice techniques, then this book is for you.
Table of Contents (12 chapters)
close
close
11
Index

Inserting content into an element

Interactive and dynamic web applications and websites not only require the web developer to be able to create DOM elements but also require the developer to be able to add dynamic content. This is easily achievable with another set of jQuery functions.

Getting ready

Create a blank HTML document named recipe-4.html, and ensure that you have the latest version of jQuery available to be included within this HTML document.

How to do it…

Learn how to dynamically add content into the DOM by performing each of the following steps:

  1. Add the following code to your newly created HTML document, which will create a simple HTML web page:
    <!DOCTYPE html>
    <html>
    <head>
       <title>Insert content into an element</title>
       <script src="jquery.min.js"></script>
       <script>
          
       </script>
    </head>
    <body>
    <div id="container">
       <p>Here is some current HTML content</p>
    </div>
    <textarea id="myTextarea"></textarea>
    </body>
    </html>
  2. Insert the following JavaScript code within the script tags in the document head. This code will inject different HTML content and elements into the DOM at various points.
    $(function(){
       //Remove the container elements current HTML
       $('#container').html("<p>I have replaced the all the HTML within the #container element</p>");
    
       //Add some more HTML to the beginning of the container element
       $('#container').prepend("<p>Another paragraph that has been prepended.</p>");
    
       //Add a button to the end of the container element after all other HTML content
       $('#container').append("<button>A Button Appended</button>");
    
       //Add some text into the text area element
       $('#myTextarea').val("Added some text using .text()");
    });

How it works…

The quickest way to add content to an element is to use the html() function. By providing this function with a string as an argument, it will replace the selected element's current DOM contents with the provided string. If no string is provided, this function will return the element's DOM contents formatted as an HTML string.

Besides replacing the content of an element, we can also use append() and prepend() to add additional content at the end and at the beginning of the current content, respectively. Additionally, we have other functions available such as text(), which will decode any HTML before it inserts the string within the element. The text() function is typically used for text areas for this reason.

Based on the HTML provided in the previous section, we can alter the content of the #container element using the jQuery functions previously discussed as follows:

$(function(){
$('#container').html("<p>I have replaced the all the HTML within the #container element</p>");

$('#container').prepend("<p>Another paragraph that has been prepended.</p>");

$('#container').append("<button>A Button Appended</button>");

$('#myTextarea').val("Added some text using .text()");
});

After each of these functions has been executed, the HTML file rendered by the browser will be transformed, which is illustrated as follows:

<!DOCTYPE html>
<html>
<head>
   <title>Insert content into an element</title>
</head>
<body>
<div id="container">
   <p>Another paragraph that has been prepended.</p><p>I have replaced the all the HTML within the #container element</p>
   <button>A Button Appended</button>
</div>
<textarea id="myTextarea">Added some text using .text()</textarea>
</body>
</html>

See also

  • Creating DOM elements
Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
jQuery 2.0 Development Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon