Search code examples
javascriptuserscripts

How to get value from page source and put the value to another text field using userscript


I want to get value from below code.

<input name="button" type="button" class="button" onclick="refresh();" id="check_code" style="font-size:15pt; width:80px; height:25px; color:white; border:none;background-color: #845f5f;text-shadow: 1px -1px 0px black;box-shadow: inset 2px -1px 4px 0px black;" readonly="readonly" oncopy="return false" value="my value">

and paste the value got into below text field.

from this
<input type="text" name="verification_code" id="verification_code" style="width:150;">

to this
<input type="text" name="verification_code" id="verification_code" style="width:150;" value="my value">

This is the code I'm working with but it's not working for some reason.

// @name     autoinput
// @match    *://MY_SERVER/*
// @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant    GM_addStyle
// ==/UserScript==
//- The @grant directive is needed to restore the proper sandbox.

function test()
{
    var element;
    element = document.getElementById("check_code").getAttribute("value");
    document.setElementById("verification_code").value = element;
}

Solution

  • document.setElementById() is not a function

    var value = document.getElementById("source").value;
    document.getElementById("dest").value = value;
    <input type="text" id="source" value="value" />
    <input type="text" id="dest" />