Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change placeholder text

How can I change the placeholder text of an input element?

For example I have 3 inputs of type text.

<input type="text" name="Email" placeholder="Some Text"> <input type="text" name="First Name" placeholder="Some Text"> <input type="text" name="Last Name"placeholder="Some Text"> 

How can I change the Some Text text using JavaScript or jQuery?

like image 398
xknozi Avatar asked Nov 22 '12 05:11

xknozi


People also ask

How do I change placeholder text?

In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector.

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. The code in this example uses a Sass function to generate code for support in older browsers as well.

Can you style placeholder text?

Definition and Usage. The ::placeholder selector selects form elements with placeholder text, and let you style the placeholder text. The placeholder text is set with the placeholder attribute, which specifies a hint that describes the expected value of an input field.

How do I change placeholder value?

The attr() method is used to set or return the specified attribute of a selected element. It takes two arguments, the first is the attribute to be set and the second is the value of the attribute to be changed to. The “placeholder” attribute controls the text that will be shown as a placeholder in an input area.


2 Answers

If you wanna use Javascript then you can use getElementsByName() method to select the input fields and to change the placeholder for each one... see the below code...

document.getElementsByName('Email')[0].placeholder='new text for email'; document.getElementsByName('First Name')[0].placeholder='new text for fname'; document.getElementsByName('Last Name')[0].placeholder='new text for lname'; 

Otherwise use jQuery:

$('input:text').attr('placeholder','Some New Text'); 
like image 196
NazKazi Avatar answered Oct 28 '22 13:10

NazKazi


This solution uses jQuery. If you want to use same placeholder text for all text inputs, you can use

$('input:text').attr('placeholder','Some New Text'); 

And if you want different placeholders, you can use the element's id to change placeholder

$('#element1_id').attr('placeholder','Some New Text 1'); $('#element2_id').attr('placeholder','Some New Text 2'); 
like image 35
arulmr Avatar answered Oct 28 '22 14:10

arulmr