Search code examples
javascripttampermonkey

auto fill form without id tampermonkey


i'm a newbie with tampermonkey and i want to auto fill a form but has no id ...i already tried some codes but doesnt work

source

<input type="text" name="user_email" class="form-control-input" value="" placeholder="Enter your Email" onselectstart="return false" onpaste="return false;" oncopy="return false" oncut="return false" ondrag="return false" ondrop="return false" autocomplete="off">

code already tested

  var mail = "test@gmail.com";
  document.getElementsByClassName('form-control-input').value = mail;

Solution

  • I personally prefer to use jQuery over pure javascript in tampermonkey because it gives me more control such as loading the script when the DOM is ready. i.e. wrap the function in a $(document).ready(function() ...

    you can try this out (make sure to change the @match)👇

    // ==UserScript==
    // @name         New Userscript
    // @namespace    http://tampermonkey.net/
    // @version      0.1
    // @description  try to take over the world!
    // @author       You
    // @match        <your site here>
    // @icon         https://www.google.com/s2/favicons?sz=64&domain=undefined.
    // @grant        none
    // @require http://code.jquery.com/jquery-3.4.1.min.js
    // ==/UserScript==
    
    (function() {
        'use strict';
    
        $(document).ready(function() {
            var mail = "test@gmail.com";
            $('[name=user_email]').val(mail);
        });
    })();