S
S
shaurmista2020-04-25 20:20:34
JavaScript
shaurmista, 2020-04-25 20:20:34

How to get the contents of a div with a wrap?

There is a div, the content of which is edited by the user himself, such as poking at it and typing
But, if it has a line break, then when its content (innerHTM, innerText) is received, undefined pops up
How to avoid this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Komchev, 2020-04-25
@Lebezniy

<div id="content_wrapper">
        <div id="content" contenteditable="true">
            Текст
            <br>
            текст
        </div>
    </div>

    <button id="get_content">Получить текст</button>

    <script>
        document.addEventListener('DOMContentLoaded', () => {
            get_content.addEventListener('click', () => {
                alert(content_wrapper.querySelector('#content').innerHTML);
            });
        });
    </script>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question