---
title: "How can you dynamically create HTML elements using jQuery?"  
description: "How can you dynamically create HTML elements using jQuery?"  
author: "Steilla Mitchel"  
published: 2023-06-09  
updated: 2023-06-10  
canonical: https://www.mindstick.com/forum/158688/how-can-you-dynamically-create-html-elements-using-jquery  
category: "jquery"  
tags: ["javascript", "jquery ui", "html form"]  
reading_time: 3 minutes  

---

# How can you dynamically create HTML elements using jQuery?

How can you dynamically create [HTML elements](https://www.mindstick.com/forum/2011/html-elements-are-ignoring-name-property) using jQuery?

## Replies

### Reply by Aryan Kumar

You can dynamically create [HTML](https://www.mindstick.com/articles/1530/design-a-simple-stylish-calculator-using-html-css-and-javascript) [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-elements) using jQuery using the following methods:

- **The append() method:** This method adds the specified HTML content to the end of the selected elements.
- **The appendTo() method:** This method adds the specified HTML content to the end of the parent element of the selected elements.
- **The html() method:** This method sets the HTML content of the selected elements.

Here is an example of how to dynamically create a div element using the append() method:

Code snippet

```plaintext
$(document).ready(function() {
  // Create a new div element
  var div = $('<div>').text('This is a dynamically created div element');

  // Append the div element to the body element
  $('body').append(div);
});
```

This code will create a new div element with the text "This is a dynamically created div element" and append it to the body element.

Here is an example of how to dynamically create a div element using the appendTo() method:

Code snippet

```plaintext
$(document).ready(function() {
  // Create a new div element
  var div = $('<div>').text('This is a dynamically created div element');

  // Append the div element to the parent element of the selected elements
  $('.my-class').appendTo(div);
});
```

This code will create a new div element with the text "This is a dynamically created div element" and append it to the parent element of all elements with the class "my-class".

Here is an example of how to dynamically create a div element using the html() method:

Code snippet

```plaintext
$(document).ready(function() {
  // Set the HTML content of the selected elements
  $('.my-class').html('<div>This is a dynamically created div element</div>');
});
```

This code will set the HTML content of all elements with the class "my-class" to a new div element with the text "This is a dynamically created div element".

You can also use the create() method to dynamically create HTML elements. The create() method is a shortcut for the append() method, but it does not require you to specify the parent element of the new element. For example, the following code is equivalent to the previous code that uses the append() method:

Code snippet

```plaintext
$(document).ready(function() {
  // Create a new div element
  var div = $('<div>').text('This is a dynamically created div element');

  // Append the div element to the body element
  $('body').append(div);
});
```

The create() method can also be used to create other types of HTML elements, such as input elements, select elements, and textarea elements. For example, the following code creates a new input element with the type "text" and the name "my-input":

Code snippet

```plaintext
$(document).ready(function() {
  // Create a new input element
  var input = $('<input>').attr('type', 'text').attr('name', 'my-input');

  // Append the input element to the body element
  $('body').append(input);
});
```


---

Original Source: https://www.mindstick.com/forum/158688/how-can-you-dynamically-create-html-elements-using-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
