skip to Main Content

I have 2 fields in a web form to fill with text from the clipboard. I managed to get 1 to work but cant the other. I’m using a Chrome addon to autofill with javascript rule tried changing getElementsByName to getElementsById but doesn’t work.

This code works:

<input id="repeatEmail" type="text" name="repeatEmail" autocomplete="chrome-off" maxlength="50" class="register__input" style="border: 1px solid rgb(170, 170, 170) !important;">
setTimeout(async function GetClip() {
  let ctext = await window.navigator.clipboard.readText()
  console.log('Clipboard: ' + ctext);
  document.getElementsByName("repeatEmail")[0].value = ctext;
}, 1000)

This code doesn’t work:

<input id="name" type="text" autocomplete="off" maxlength="28" class="register__input" style="border: 1px solid rgb(170, 170, 170) !important;">
setTimeout(async function GetClip() {
  let ctext = await window.navigator.clipboard.readText()
  console.log('Clipboard: ' + ctext);
  document.getElementsByName("name")[0].value = ctext;
}, 1000)

3

Answers


  1. There isn’t any attribute named 'name' in your code. getElementsByName looks for an attribute called 'name'. You need something like-

     let c = document.getElementsByName("name")[0];
     
     console.log(c)
    <input id="name" name='name' type="text" autocomplete="off" maxlength="28" class="register__input" style="border: 1px solid rgb(170, 170, 170) !important;">
    Login or Signup to reply.
  2. try this

    <input id="name" type="text" autocomplete="off" maxlength="28" class="register__input" style="border: 1px solid rgb(170, 170, 170) !important;">
    
    
    setTimeout(async function GetClip() {
        let ctext = await window.navigator.clipboard.readText()
        console.log('Clipboard: ' + ctext);
        document.getElementById("name").value = ctext;
      }, 1000)
    

    you can’t use getElementsById because id in element should be have 1 id (unique) and document.getElementsById is not a function so you must change getElementsById to getElementById

    Login or Signup to reply.
  3. The second code doesn’t work because there is no attribute called ‘name’ in the input, you can use getElementById instead of getElementsByName or use

    document.querySelector('#name')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search