Home  >  Article  >  Web Front-end  >  How to Append Text to a DIV Element with AJAX?

How to Append Text to a DIV Element with AJAX?

DDD
DDDOriginal
2024-11-15 10:55:02713browse

How to Append Text to a DIV Element with AJAX?

Appending Text to a DIV Element with AJAX

When using AJAX to dynamically populate a DIV element with data from JavaScript, it's common to want to append new data without losing existing content. To achieve this, follow the below steps:

  1. Identify the DIV element: Use document.getElementById('divID') to retrieve the DIV with the specified ID.
  2. Append new content: Utilize the innerHTML property to add text to the DIV. To append new content without overwriting existing text, use the = operator:
var div = document.getElementById('divID');

div.innerHTML += 'Extra stuff';

This code will append the text "Extra stuff" to the existing content of the DIV with the ID "divID". The innerHTML property can also be used to replace existing content if desired.

The above is the detailed content of How to Append Text to a DIV Element with AJAX?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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