Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set placeholder with JS

Please take a look at this fiddle:

http://jsfiddle.net/KA85s/3/

JQ-UI sets first option as default. I don't want it to be happened: instead I want to set HTML5 placeholder.

How can I set HTML 5 placeholder (instead of first available <option>) for these JS generated inputs?

enter image description here

like image 838
heron Avatar asked Feb 20 '12 06:02

heron


People also ask

What is placeholder in JS?

The placeholder attribute specifies a short hint that describes the expected value of a text field (e.g. a sample value or a short description of the expected format). The short hint is displayed in the text field before the user enters a value.

How do I change placeholder text?

The “placeholder” property is used to get or set the placeholder of an input text. This can be used to change the placeholder text to a new one. The element is first selected using a jQuery selector. The new placeholder text can then be assigned to the element's placeholder property.

How do I add a placeholder 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.

How do I change placeholder text in CSS?

Change Input Placeholder Text with CSS You can use the ::placeholder pseudo-element to change the styles of the placeholder text, which includes the ability to change the background.


1 Answers

Use:

input.attr("placeholder", value);

Updated jsfiddle:

http://jsfiddle.net/Meligy/KA85s/5/

In the example I just replaced your val() call, obviously, you can completely remove the part from the function and add it separately with any text you want.

like image 117
Meligy Avatar answered Oct 04 '22 06:10

Meligy