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
There isn’t any attribute named
'name'
in your code.getElementsByName
looks for an attribute called'name'
. You need something like-try this
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
The second code doesn’t work because there is no attribute called ‘name’ in the input, you can use
getElementById
instead ofgetElementsByName
or use