Web Tools Table Maker3schools TranslateImage CompressorFavicon Icon GeneratorCrop & Resize Image
Apu
Apu July 04, 2023 › #HowTo #Javascript

How to display text in div using JavaScript

To display text in a div element using JavaScript, you can use the textContent property of the div element. The example is given below.

const myDiv = document.querySelector('.my-div');
myDiv.textContent = "New Text"

In this example, we first retrieve the div element with the class "my-div" using the querySelector() method. Then, we set the textContent property of the div element to "New Text". This will replace any existing text content in the div element with the new text.

Alternatively, you can also use the innerHTML property to set the HTML content of the div element.

const myDiv = document.querySelector('.my-div');
myDiv.innerHTML = "<p>New Text</p>";

In this example, we set the innerHTML property of the div element to a paragraph element containing the text "New Text". This will replace any existing content in the div element with the new HTML content.

How to display text in div using JavaScript #

  1. Get the div element by its id using document.getElementById() method and set its text content using the textContent property.
    <div id="my-div">This is an element.</div>
    
      <script>
     const myDiv = document.getElementById('my-div');
     myDiv.textContent = "New Text";
    </script>
  2. Get the div element by its class using document.querySelector() method and set its HTML content using the innerHTML property.
    <div id="my-div">This is an element.</div>
      
      <script>
     const myDiv = document.querySelector('.my-div');
     myDiv.innerHTML = "<p>New Text</p>";
    </script>
  3. Use createElement() and createTextNode() methods to create new text node and append it to the div element using the appendChild() method.
  4. Use insertAdjacentHTML() method to insert HTML content into a specified position around the div element.
save
listen
AI Answer
2 Answers
Write Your Answer
  1. There are several ways to display text in a <div> using JavaScript.

    Using innerHTML property:
    <script>
    document.getElementById('output').innerHTML = 'Hello, World!';
    </script>


    Creating a text node and appending it to the  <div>:
    <script>
    let text = document.createTextNode('Hello, World!');
    document.getElementById('output').appendChild(text);
    </script>


    Using textContent property:
    <script>
    document.getElementById('output').textContent = 'Hello, World!';
    </script>
    • You can update the text dynamically based on user input using event listeners or by using JavaScript functions.
      https://www.3schools.in/p/embed.html?q=CjxpbnB1dCBpZD0iaW5wdXQiPgo8cCBpZD0ib3V0cHV0Ij48L3A+CjxzY3JpcHQ+CiB2YXIgaW5wdXRFbGVtZW50ID0gZG9jdW1lbnQuZ2V0RWxlbWVudEJ5SWQoJ2lucHV0Jyk7CiB2YXIgb3V0cHV0RWxlbWVudCA9IGRvY3VtZW50LmdldEVsZW1lbnRCeUlkKCdvdXRwdXQnKTsKCiBpbnB1dEVsZW1lbnQuYWRkRXZlbnRMaXN0ZW5lcignaW5wdXQnLCBmdW5jdGlvbigpIHsKICB2YXIgaW5wdXRWYWx1ZSA9IGlucHV0RWxlbWVudC52YWx1ZTsKICBvdXRwdXRFbGVtZW50LnRleHRDb250ZW50ID0gaW5wdXRWYWx1ZTsKIH0pOwo8L3NjcmlwdD4=
      In the above example, we have an input field with the id input where the user can enter their text. Whenever the user types in the input field, the event listener captures the input and updates the text in the <div> with the id output accordingly.
    Reply Delete
    Share
    Reply Delete
    Share
loading
back
View All