Home > Web Front-end > JS Tutorial > body text

How to combine multiple elements using JavaScript and append the result to a div?

PHPz
Release: 2023-09-08 14:37:09
forward
1355 people have browsed it

如何使用 JavaScript 组合多个元素并将结果附加到 div 中?

Sometimes, we need to use JavaScript to manipulate HTML elements. Therefore, we can use JavaScript to add or remove HTML elements. This tutorial will teach us to combine multiple HTML elements in a shot using JavaScript.

Sometimes we need to display some HTML elements to the user when they click a button or a specific event trigger. So we can use the method below to combine multiple elements and append the result to a div element using JavaScript.

Use innerHTML attribute

innerHTML, as the name suggests, allows us to set the HTML of any specific element using JavaScript. Using the assignment operator with the innerHTML attribute replaces the HTML of a specific element.

When we use the = operator with the innerHTML attribute, we can append multiple elements to a specific HTML element.

grammar

You can combine multiple elements and append them to a div element using the innerHTML attribute following the following syntax.

test_div.innerHTML += html;
Copy after login

In the above syntax, test_div is an HTML element accessed through JavaScript.

Example

In the example below, we will do five for loop iterations. We use the innerHTML attribute to append some HTML to the div element on each loop iteration.

<html>
<head>
   <style>
      button {
         font-size: 1.3rem;
         background-color: aqua;
         border-radius: 10px;
         color: blue;
         margin: 10px;
      }
   </style>
</head>
<body>
   <h3>Using the <i> innerHTML property </i> to append multiple HTML elements to the particular HTML element</h3>
   <div id = "test_div"> This is the HTML div element.</div>
   <button onclick = "appendEle()"> Append elements</button>
   <script>
      function appendEle() {
         let test_div = document.getElementById("test_div");
         for (let i = 0; i < 5; i++) {
            test_div.innerHTML += "<p> digit is " + i + " </p>";
         }
      }
   </script>
</body>
</html>
Copy after login

Use JQuery’s append() method

We can use JQuery's append() method to append HTML to a specific element. We can use the append() method multiple times to append multiple elements to a specific HTML element.

grammar

Users can use jQuery's append() method to append multiple HTML elements to a specific HTML element according to the following syntax.

$('#content').append(html)
Copy after login

In the above syntax, html is a line of html, containing multiple or single elements, to be appended to the end of the HTML element.

Example

In the example below, when the user clicks the button, it calls the appendHTML() function. In the appendHTML() function, we append multiple HTML elements to a specific element using a loop. Users can see that we use the JQuery append() method to append new HTML elements on each loop iteration.

<html>
<head>
   <style>
      div {
         font-size: 1.5rem;
         background-color: yellow;
         color: black;
         width: 250px;
      }
   </style>
   <script src = "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"> </script>
</head>
<body>
   <h3>Using the <i>JQuery append()</i> method to append multiple HTML elements to the particular HTML element </h3>
   <div id = "content"> testing content. </div><br>
   <button onclick = "appendHTML()"> Append elements </button>
   <script>
      function appendHTML() {
         for (let i = 0; i < 10; i++) {
            $('#content').append('<div> This div is appended! </div>')
          }
      }
   </script>
</body>
</html>
Copy after login

In the above output, the user can observe that when the user clicks on the button, it appends the HTML element to the div element with the id "content".

Use JavaScript’s after() method

JavaScript includes the after() method for adding HTML elements after a specific element. We can pass comma separated lines of HTML or create after elements in JavaScript as arguments to the after() method.

grammar

Users can follow the syntax below to append multiple elements to an HTML element using JavaScript's after() method instead of combining them into a single element.

div_Element.after(elements);
Copy after login

parameter

  • elements - They are multiple comma separated html elements added after a specific HTML element.

Example

In the example below, the concatElements() function is executed when the user clicks the button. In the concatElements() function, we create an HTML element using the createElement() method and add html to it using the innerHTML attribute.

After that, we pass element1 and element2 as parameters of the after() method to append them after the div element.

<html>
<head>
   <style>
      div {
         font-size: 1.5rem;
         background-color: yellow;
         color: black;
         width: 250px;
      }
      p {
         font-size: 1rem;
         background-color: blue;
         color: white;
         width: 250px;
         padding: 5px;;
      }
   </style>
</head>
<body>
   <h3>Using the <i>after()</i> method to append multiple HTML elements to the particular HTML element</h3>
   <div id = "content"> testing content. </div>
   <button onclick = "concatElements()"> Combine elements </button>
   <script>
      function concatElements() {
         let element1 = document.createElement('p');
         element1.innerHTML = "This is a first element!";
         let element2 = document.createElement('p');
         element2.innerHTML = "This is a second element!";
         let div_Element = document.getElementById('content');
         div_Element.after(element1, element2);
      }
   </script>
</body>
</html>
Copy after login

Users learned three ways to combine multiple HTML elements in JavaScript and append the resulting element to any HTML element. In the first method, the user can store multiple elements in a single variable using = operator, and then, we can assign the value of the resulting element to the innerHTML attribute.

The above is the detailed content of How to combine multiple elements using JavaScript and append the result to a div?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template