Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the "placeholder" attribute of HTML5 input elements dynamically using Javascript

I'm trying to dynamically update the HTML5 placeholder attribute of a text field using jQuery.

$("textarea").attr("placeholder", "New placeholder text"); 

From Firebug, I can observe that the placeholder attribute is indeed changing. But in the rendered textarea element, it stays the same. Any suggestions?

like image 794
cemregr Avatar asked Oct 12 '10 21:10

cemregr


People also ask

How do I change a dynamic placeholder?

A placeholder is meant to describe the generic pattern of expected input, not some specific value. If you have a useful default value e.g. from previous input, make it the default by putting it into the value attribute (property) and omit the placeholder attribute as not needed.

How do I change placeholder text in HTML?

<input type="text" placeholder="A red placeholder text..">

How do you target a placeholder in JavaScript?

To change the placeholder text of an input element with JavaScript, we can set the placeholder property of an element. Then we can set the placeholder attribute value for each input element by writing: document. getElementsByName('Email')[0].

What is placeholder attribute in HTML5?

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


2 Answers

try this :

$('#inputTextID').attr("placeholder","placeholder text"); 
like image 76
tilak Avatar answered Sep 20 '22 17:09

tilak


I think you have to do that :

$("textarea").val(''); $("textarea").attr("placeholder", "New placeholder text"); 
like image 23
xavok Avatar answered Sep 20 '22 17:09

xavok