1

I'm trying to put 3 or 4 buttons that would help registering users to enter their e-mail addresses. Basically what I need is when they click "@gmail.com" button, their address will be completed with that. I ended up with a code like this:

function insertText(elemID, text) {
  var elem = document.getElementById(elemID);
  elem.innerHTML += text;
}
<form>
  <textarea id="txt1"></textarea>
  <input type="button" value="Insert some text" onclick="insertText('txt1', 'Hello');">
</form>

However as you can try and see, it's not working when a user enters some text inside and clicks the button. I want to resolve this issue with minimum amount of script and preferably without jQuery.

Note: I will place this code snippet inside a block, but the textarea might be not in the same block. Is that possible to still make use of it?

2 Answers 2

1

A <textarea> element uses value not innerHTML:

function insertText(elemID, text) {
  var elem = document.getElementById(elemID);
  elem.value += text;
}
Sign up to request clarification or add additional context in comments.

3 Comments

this is working perfectly fine, thanks for your help. but i'm unable to make it work in my page, since the text field is outside of the area i will put the code, in other words i won't create the text but will make use of an existing one. how can I achieve this?
"i won't create the text" - are you referring to a string or to the textarea element?
i meant textarea, sorry if I couldn't explain it well. I will put this script and the button on an existing page, so button and text area will be in different div's. I tried to put the button and script, and it didn't worked, so I think the reason is their placement. I think I have no choice of putting them together. If that gives a clue, I'm using Ultimate Member and trying to modify their registration form with buttons for common mail domains, which will be appended to the given text on button click.
0

Change elem.text to elem.value in your code

function insertText(elemID, text) {
  var elem = document.getElementById(elemID);
  elem.value += text;
}

1 Comment

this is working perfectly fine, thanks for your help. but i'm unable to make it work in my page, since the text field is outside of the area i will put the code, in other words i won't create the text but will make use of an existing one. how can I achieve this?

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.