Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding input prompts to HTML form fields

I'm looking for a good way to add input prompts to my HTML form fields- The same way StackOverflow uses light gray text as prompts within all of its text fields.

Figured there would be a jQuery plugin out there, but so far haven't found anything good. Anybody?

like image 519
Yarin Avatar asked May 18 '11 16:05

Yarin


People also ask

How do you ask for input in HTML?

The prompt() method displays a dialog box that prompts the user for input. The prompt() method returns the input value if the user clicks "OK", otherwise it returns null .

Is there any way to create prompt with two input fields?

This is not possible with an OS or native browser window popping up. You will have to create a custom overlay dialog. I would advise using a library like jQuery UI to do this. You can then customize whatever is in the popup.

Which attribute is required for all input fields in a HTML form?

The required attribute is a boolean attribute. When present, it specifies that an input field must be filled out before submitting the form.


2 Answers

See the answers to this question: Jquery default value in password field

In html5 you can do this:

<input type="text" placeholder="Default Value"/>

This is what SO does if you view the search bar on top:

<input name="q" class="textbox" tabindex="1" onfocus="if (this.value=='search') this.value = ''" type="text" maxlength="140" size="28" value="search">
like image 185
Naftali Avatar answered Sep 23 '22 13:09

Naftali


If you mean having light grey text inside the form field, you can use the placeholder attribute in recent browsers:

<input type="text" placeholder="This text will appear inside the form field until the user focuses it">

I don’t know of any packaged jQuery plug-ins that mimic this functionality in browsers that don’t support placeholder, but here’s an example of how to do it yourself in jQuery:

  • http://web.enavu.com/design/css/use-html5-placeholder-input-attribute-today-using-jquery/
like image 28
Paul D. Waite Avatar answered Sep 21 '22 13:09

Paul D. Waite