Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Html placeholder text in a textarea form

On one of my websites I have created a form that collects the persons name, email and a description of their idea.

I limited the characters of the description to 500 characters as I don't want to read a ton and I figured out how to have the text appear in the textarea before the user inputs what they want.

Currently the user has to delete "Description of your idea" themselves but I want to add the placeholder class where it deletes what I have written in the textarea when they click the textarea

I have looked on a few sites and couldn't figure out how to use it I placed it in my code, but usually the class just appeared as text inside my textarea.

Any help on using this class would be great thank you

Here is what I have written

Inside the head tags

<script language="javascript" type="text/javascript">
function limitText(limitField, limitCount, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } else {
        limitCount.value = limitNum - limitField.value.length;
    }
}
</script>

Inside the body tags

<form name="form1" method="post" action="ideas.php"> 
Your Name: &nbsp;<input type="text" name="name"><br>
Your Email: &nbsp;<input type="text" name="email"<br>
<textarea name="desc" cols=50 rows=10 onKeyDown="limitText(this.form.desc,this.form.countdown,500);" 
onKeyUp="limitText(this.form.desc,this.form.countdown,500);">Description of your idea</textarea><br>
<font size="1">(Maximum characters: 500)<br>
You have <input readonly type="text" name="countdown" size="3" value="500"> characters left.</font>
<br>
<input type="submit" name="Submit" value="Submit!"> </form>
like image 548
GFlam Avatar asked Feb 16 '11 20:02

GFlam


People also ask

How do you add a placeholder to a text box in HTML?

If you want to set a hint for text area or input field, then use the HTML placeholder attribute. The hint is the expected value, which gets displayed before the user enters a value, for example, name, details, etc.

Why my placeholder is not working in HTML?

If you have an input in your form and placeholder is not showing because a white space at the beginning, this may be caused for you "value" attribute. In case you are using variables to fill the value of an input check that there are no white spaces between the commas and the variables.

Can I use textarea in a form in HTML?

Yes you can use textarea tags outside of a form and they will display and allow text to be inserted and edited, but not being tied to a form their uses will likely be limited.

What is placeholder in textbox?

Definition and Usage The placeholder attribute specifies a short hint that describes the expected value of a input field / textarea. The short hint is displayed in the field before the user enters a value.


1 Answers

There is a feature in HTML5 called 'placeholders', which produces exactly this feature without you having to do any coding at all.

All you need to do is add a placeholder attribute to your form field, like so:

<input type='text' name='name' placeholder='Enter your name'>

Sadly, of course, only a few browsers currently support it, but give it a go in Safari or Chrome to see it in action. The good news is that it is being added to virtually all browsers in the near future.

Of course, you still need to cater for users with older browsers, but you may as well make use of the feature in browsers that can use it.

A good way to deal with it is to use the placeholder attribute, and only fall back to the Javascript solution if the browser doesn't support the feature. The Javascript solution can take the text from the placeholder attribute, so you only need to specify it in one place.

See this page for how to detect whether the placeholder feature is supported: http://diveintohtml5.ep.io/detect.html

(or, as it says on that page, just use Modernizr)

The Javascript fall-back code is fairly simple to implement. Exactly how you do it would depend on whether you want to use JQuery or not, but here are links to a few examples:

  • http://www.morethannothing.co.uk/2010/01/placeholder-text-in-html5-a-js-fallback/
  • http://www.hagenburger.net/BLOG/HTML5-Input-Placeholder-Fix-With-jQuery.html

And of course Google will give you loads more if you search for html5 placeholder fallback or something similar.

Hope that helps.

like image 196
Spudley Avatar answered Nov 01 '22 15:11

Spudley