0
<input type="text" name="turns" size="3" maxlength="4" value="99">

The max length there is 4. Im wondering what I could put into tampermonkey to make it automatically change max length to something longer, say 6.

11
  • The max length where?
    – Barmar
    Jun 28, 2014 at 0:48
  • Fixed, code stuff didnt show up Jun 28, 2014 at 0:49
  • Use the {} tool in the SO editor to mark code.
    – Barmar
    Jun 28, 2014 at 0:51
  • Loop over all the input elements, and do element.setAttribute('maxlength', 6);.
    – Barmar
    Jun 28, 2014 at 0:53
  • And how would you tell someone to do that, if they had no idea what you mean haha Jun 28, 2014 at 1:04

1 Answer 1

0

If I wanted to do exactly what you ask, I would say

var i,myInputs = document.getElementsByName("turns");

for (i=0;i<myInputs.length;i++) {
  if ( (myInputs[i].tagName.toLowerCase()=="input")
    && (myInputs[i].type.toLowerCase()=="text")
  ) myInputs[i].setAttribute("maxlength","6");
}

This is a lot of code; that's because there might be more than one element with the same name, and they might not be <input> elements, or they might have a type other than "text". If you don't care about potential errors, just say

document.getElementsByName("turns")[0].maxLength = 6;

which will work fine as long as the HTML you refer to appears exactly once on the page.

2
  • Thinking about it, just do try{document.getElementsByName("turns").forEach(function(x){x.maxLength=6})} catch(e) {}.
    – bobtato
    Jun 28, 2014 at 1:57
  • The first one you put in worked great, thanks a lot! Jul 11, 2014 at 12:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.