Dom insert the cursor into data text field

63 views Asked by At

I'm a beginner to HTML DOM and what I want to achieve is focus textarea so that cursor will be placed in it to type.

<div class="_4bl9">
  <div class=" _30z _4h96">
    <textarea name="xhpc_message_text" placeholder="Write something..." class="_4h98"></textarea>
    <input value="" name="xhpc_message" type="hidden">
  </div>
</div>

I was using this code before and it was working but not anymore.

document.getElementsByName('xhpc_message')[0].focus()

Can any one suggest me other way to do? Thanks.

2

There are 2 answers

0
Ravi MCA On BEST ANSWER

document.getElementsByName('xhpc_message_text')[0].focus()
<div class="_4bl9">
  <div class=" _30z _4h96">
    <textarea name="xhpc_message_text" placeholder="Write something..." class="_4h98"></textarea>
    <input value="" name="xhpc_message" type="hidden">
  </div>
</div>

There is a typo in your script. It's not xhpc_message it is xhpc_message_text

0
Lukas Kolletzki On

I assume you want to place the cursor in the textarea, instead of the input field, because the input field is hidden.

If that's what you want to do, the name you use to select the element in your previous code isn't correct, it should be xhpc_message_text.

The following example places the cursor in the textarea:

document.getElementsByName('xhpc_message_text')[0].focus();
document.getElementsByName('xhpc_message_text')[0].select();