你說:
How do I get input text in the following HTML element? <div class="text-content" id="source-text" contenteditable="true"></div>
ChatGPT 說:

To get the input text from a <div> element with contenteditable="true", you can use JavaScript to access the innerText or textContent of the element.

Here’s how you can do it:

Using innerText:

javascript
const sourceText = document.getElementById('source-text').innerText; console.log(sourceText);

Or using textContent:

javascript
const sourceText = document.getElementById('source-text').textContent; console.log(sourceText);

Both innerText and textContent will give you the text inside the editable div. The main difference is that innerText respects styling (like hidden elements) and line breaks, while textContent just retrieves the raw text content, regardless of styling.

If you need to get the text when an event occurs, like when the user finishes typing, you can use an event listener:

javascript
document.getElementById('source-text').addEventListener('input', function() { const sourceText = this.innerText; // or this.textContent console.log(sourceText); });

This way, you'll capture the text content every time the user types into the div.


ChatGPT 可能會發生錯誤。請查核重要資訊。