A 'driven' software engineer with a passion for cars and tabletop games. Get it, driven? Because cars and... Okay, I'll stick to writing code instead of puns. π
This was very clear, clever, and simple! It's one of those form quirks that we've all seen at some point but not all of us have implemented. I like that the text warning pops up towards the end to help with accessibility too.
French web developer mainly but touches everything. Volunteer admin mod here at DEV. I learn AI and Astro at this moment and databases. β Addict to Cappuccino and Music
I would probably caution people not to do this unless there's a good reason though. It can be very frustrating to find a form which asks you to enter, say, the steps that caused the problem you've had, and to not have enough room to type it all.
In most situations, your application should accept such a large amount of text that displaying a message is irrelevant.
Yeah, it most definitely isn't for all use cases. But It can be useful for a social media post, or when the information requested isn't allowed to be too lengthy.
Professional Data Engineer/Business Intelligence Developer. I build build and test various data pipelines between production and analytics databases. Additionally, I design dashboards.
That was really well explained, thank you.
My site has a couple of text areas so i ended up adding a for loop to the javascript which detected all text areas and looped though them. Seems to work OK.
Let me know if you would like me to post the extended code.
Sure thing. Now i will start by saying I am still very new to all this. But here goes.
Here is the HTML for the textareas:
//Text area 1
<labelfor="textArea1"class="requiredfield">This is the text area 1 text box</label><textareaid="textArea1"name="textArea1"spellcheck="true"rows="4"cols="50"maxlength="500"></textarea><divid="progress-textArea1"><divid="progress-bar-textArea1"></div><pid="remaining-chars-textArea1"></p></div>
...
//Text area 2
<labelfor="textArea2"class="requiredfield">This is the text area 2 text box</label><textareaid="textArea2"name="textArea2"spellcheck="true"rows="4"cols="50"maxlength="500"></textarea><divid="progress-textArea2"><divid="progress-bar-textArea2"></div><pid="remaining-chars-textArea2"></p></div>
Here is the updated CSS:
/*This is for the textArea character counter and progress bars*/#progress-textArea1,#progress-textArea2{width:100%;height:7px;box-shadow:inset1px1px2px#ccc;border:1pxsolid#bbb;border-radius:15px;}#progress-bar-textArea1,#progress-bar-textArea2{height:100%;}#remaining-chars-textArea1,#remaining-chars-textArea2{font-size:11px;color:#b62020;margin-top:3px;float:right;display:none;}
And here is the updated Javascript:
/*
This section relates to the textArea character counter and progress bar
It loops through all textarea elements and processes each within the for loop.
This requires the progressBar IDs to be appended with the textArea field ID in the HTML and CSS
*/letallTextArea=document.querySelectorAll("textarea");letnumAllTextArea=allTextArea.length;for (leti=0;i<=numAllTextArea;i++){letfieldToProcess=allTextArea[i].id;letprogressBarId="progress-bar-"+fieldToProcess;letremCharsId="remaining-chars-"+fieldToProcess;lettextArea=document.getElementById(fieldToProcess);letprogressBar=document.getElementById(progressBarId);letremChars=document.getElementById(remCharsId);functioncharCounter(inputField){letmaxLength=inputField.getAttribute("maxlength");letcurrentLength=inputField.value.length;letprogressWidth=(currentLength/maxLength)*100;progressBar.style.width=`${progressWidth}%`;remChars.style.display="none";if (progressWidth<=60){progressBar.style.backgroundColor="#00594c";}elseif (progressWidth>60&&progressWidth<85){progressBar.style.backgroundColor="#ffd602";}else{progressBar.style.backgroundColor="#b8232f";}remChars.innerHTML=`${maxLength-currentLength} characters left`;remChars.style.display="block";}textArea.oninput=()=>charCounter(textArea);}
That's quite nice, but I do find some little issues that might arise with this approach. The first issue being that the warning text doesn't popup at the end, instead it's visible from start to finish, which doesn't look right. Also, I added two new texareas, but they didn't seem to be functional as the first two. Overall, having a function Inside a for loop like that isn't a very neat approach, makes the code unscalable and the function hard to maintain.
A cleaner and easier approach to achieve this for multiple textareas in the document is to separate the function from the for loop.
Now, you can add as many texareas as you want, without needing to modify the JavaScript again.
You can check it out in the codepen below, where I added two new textareas with different maxlengths:
Interesting that progress tag still not so easy to use for this use case that fits into purpose of element only because getting it styled properly is not so easy as it should.
I initially started with the progress tag, because I wanted to stick to semantics, but unfortunately it's styling capabilities and browser inconsistencies we're just too much of a hassle. They really should make the progress element more style flexible.
It seems that simplest way to achieve that would be adopting that webkit kind of implementation as default and wait till it will get enough distribution to be considered "widespread enough". Mozilla variant is more limited, sadly.
Hello, I liked your code and recently I have been dedicating myself to studying Stimulus.js. Therefore, I decided to create a version of your code for Stimulus. For convenience, I used Tailwind CSS
I hope it can be useful to someone.
We're a place where coders share, stay up-to-date and grow their careers.
This was very clear, clever, and simple! It's one of those form quirks that we've all seen at some point but not all of us have implemented. I like that the text warning pops up towards the end to help with accessibility too.
Thank youu :)
Your post is constructive and very clear, thanks ! π
Thank you π
Thanks for the tutorial, I'll try to implement it into a small personnal project
You're welcome :) Awesome
It's super cool
Thank you :) :)
very good and helpful, thanks.
Nice article π
Thank you :)
Nice, nice.
I would probably caution people not to do this unless there's a good reason though. It can be very frustrating to find a form which asks you to enter, say, the steps that caused the problem you've had, and to not have enough room to type it all.
In most situations, your application should accept such a large amount of text that displaying a message is irrelevant.
I have use it in my application, to limit the body of SMS text message, because the API accept limited text..
Yeah, it most definitely isn't for all use cases. But It can be useful for a social media post, or when the information requested isn't allowed to be too lengthy.
awesome
Thankss :)
That was really well explained, thank you.
My site has a couple of text areas so i ended up adding a for loop to the javascript which detected all text areas and looped though them. Seems to work OK.
Let me know if you would like me to post the extended code.
Cheers
That's a great workaround π, and sure you can post the extended code
Sure thing. Now i will start by saying I am still very new to all this. But here goes.
Here is the HTML for the textareas:
Here is the updated CSS:
And here is the updated Javascript:
That's quite nice, but I do find some little issues that might arise with this approach. The first issue being that the warning text doesn't popup at the end, instead it's visible from start to finish, which doesn't look right. Also, I added two new texareas, but they didn't seem to be functional as the first two. Overall, having a function Inside a for loop like that isn't a very neat approach, makes the code unscalable and the function hard to maintain.
A cleaner and easier approach to achieve this for multiple textareas in the document is to separate the function from the for loop.
Here is my updated counter function:
This way the function logic has been separated and can be used anywhere.
Here is the forEach loop to get all the texareas in the document and apply the counter function to them.
Now, you can add as many texareas as you want, without needing to modify the JavaScript again.
You can check it out in the codepen below, where I added two new textareas with different maxlengths:
Interesting that
progresstag still not so easy to use for this use case that fits into purpose of element only because getting it styled properly is not so easy as it should.I initially started with the progress tag, because I wanted to stick to semantics, but unfortunately it's styling capabilities and browser inconsistencies we're just too much of a hassle. They really should make the progress element more style flexible.
It seems that simplest way to achieve that would be adopting that webkit kind of implementation as default and wait till it will get enough distribution to be considered "widespread enough". Mozilla variant is more limited, sadly.
Hello, I liked your code and recently I have been dedicating myself to studying Stimulus.js. Therefore, I decided to create a version of your code for Stimulus. For convenience, I used Tailwind CSS
I hope it can be useful to someone.