1

When pressing Add foo, the textarea get's filled with string: foo.

However, after typing something in my textarea, my Add foo button doesn't work anymore.

<button onclick="document.getElementById('content').append('foo \n\n')">Add foo</button>

<textarea name="content" id="content" cols="30" rows="10"></textarea>

3 Answers 3

2

For input elements, you can use += to append to the value.

Here is a cleaner version

content = document.querySelector("#content");
document.querySelector("button").addEventListener("click",function(){
   content.value += " ADD FOO";
});
<button>Add foo</button>

<textarea name="content" id="content" cols="30" rows="10"></textarea>

Here is a version using your code

<button onclick="document.getElementById('content').value += 'foo \n\n'">Add foo</button>

<textarea name="content" id="content" cols="30" rows="10"></textarea>

Sign up to request clarification or add additional context in comments.

Comments

0

Since textarea is a form element, you should append to its value attribute:

function addFoo(){
  let ta = document.getElementById('content')
  ta.value += 'foo \n\n'
}
<button onclick="addFoo()">Add foo</button>

<textarea name="content" id="content" cols="30" rows="10"></textarea>

Comments

0

you have to get the content of your element then add what you want

<body class="flex justify-center items-center bg-purple-700">
<button onclick="document.getElementById('content').value=document.getElementById('content').value+'foo \n\n'">Add foo</button>

<textarea name="content" id="content" cols="30" rows="10"></textarea>
</body>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.