1

I am trying to edit the following input form to change the maxlength value.

<div id="chat-input">
    <form class="chat-input-form" onsubmit="return false">
        <input id="chat-input-field" value="" placeholder="Click here to join the conversation" maxlength="256" type="text">
    </form>
</div>

Say I want to change the maxlength of the input to 300, how would I go about doing this client side? I know I can use inspect element, but I am trying to do it with a JavaScript file.

2
  • Isn't it just document.getElementById('chat-input-field').maxLength = xxx; ? Commented May 8, 2014 at 18:52
  • This was the first thing I tried, but I am still limited to 256. Commented May 8, 2014 at 18:56

1 Answer 1

2

Pretty simple:

document.getElementById('chat-input-field').maxLength = 300

JSFiddle.

Don't forget to wrap the code inside of a document ready function. Something like this should do it, but stick to jQuery $(document).ready() if you want to support stone age browsers too.

document.addEventListener('DOMContentLoaded', function(){
   document.getElementById('chat-input-field').maxLength = 300
});

I don't think I would need to add the DOMContentLoaded because the javascript file I am using is only added after the website is finished loading.

Then try this.

console.log(document.getElementById('chat-input-field'));

document.addEventListener('DOMContentLoaded', function(){
   console.log(document.getElementById('chat-input-field').maxLength);
});

Expected result in console:

null

256

Sign up to request clarification or add additional context in comments.

5 Comments

I don't think I would need to add the DOMContentLoaded because the javascript file I am using is only added after the website is finished loading.
As far as I know, the only difference between the working JSfiddle and your test is the onLoad statement. This might be suggesting you something.
Ok I was wrong, the DOMContentLoaded does make it work. Thanks for your help!
Ok it was working for a minute when I first tried it, but now I can't get it to work at all. I even tried doing the console log commands, but instead off undefined and 256, I get [object HTMLInputElement] which gives me all this information I do not need. Did I miss a step this time? I figured all I needed to use to run it would be document.addEventListener('DOMContentLoaded', function(){ document.getElementById('chat-input-field').maxLength = 300; }); Is that wrong?
yes, that's all you need. Try to clear the browser cache and refresh the page.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.