gpt-4.1-mini-2025-04-14· OpenAI
WildChat-4.8M (AI2)
js add text
- User
js add text
- Assistant
To add text in JavaScript, you can use different methods depending on where and how you want to add the text. Here are some common ways to add text to a webpage with JavaScript:
1. Using
textContentThis sets or gets the text content of an element.
<div id="myDiv"></div> <script> const div = document.getElementById('myDiv'); div.textContent = "Hello, world!"; </script>2. Using
innerTextSimilar to
textContent, but respects styles likedisplay:none.<div id="myDiv"></div> <script> const div = document.getElementById('myDiv'); div.innerText = "Hello, world!"; </script>3. Using
innerHTML(for adding HTML content)If you want to add HTML elements along with text, use
innerHTML.<div id="myDiv"></div> <script> const div = document.getElementById('myDiv'); div.innerHTML = "<strong>Hello</strong>, world!"; </script>