0

I'm trying to auto-change the maximum value of an input field automatically using a tampermonkey script, this is the code of said input feild:

<input id="nick" class="form-control" placeholder="Nick" maxlength="15" autofocus>

The maximum value of the input field is 15, im trying to have it auto-change it to 100 every time i reload the page by using this code:

document.getElementById("nick").value = "100";

For some reason this does not work, i tried setting the "id" to "maxLength" instead but then i get this error, "Cannot set property 'value' of null" How can I get it to change the input field to 100 every time but not get this error? Thanks for your time.

m-byte
  • 75
  • 1
  • 6
  • 1
    where are you including the script? – brk Aug 25 '16 at 17:31
  • The script either has to come *after* you've declared the `input` or you need to register a callback for when the DOM is ready. – Mike Cluck Aug 25 '16 at 17:34
  • 1
    Possible duplicate of [Why does jQuery or a DOM method such as getElementById not find the element?](http://stackoverflow.com/questions/14028959/why-does-jquery-or-a-dom-method-such-as-getelementbyid-not-find-the-element) – Teemu Aug 25 '16 at 17:36
  • Shouldn't you be changing `maxLength`, not `value`? Seems like a typo to me. – Mike Cluck Aug 25 '16 at 17:39

1 Answers1

0

Use maxLength property, not value.

HTMLInputElement.maxLength: The maximum number of characters that the textbox allows to be entered.

document.getElementById("nick").maxLength = "100";
<input id="nick" class="form-control" placeholder="Nick" maxlength="15" autofocus>
Rayon
  • 34,175
  • 4
  • 40
  • 65