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?
In most browsers, the placeholder text is grey. To change this, style the placeholder with the non-standard ::placeholder selector.
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.
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.
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.
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');
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With