1

I want to set the placeholder value to an input I am creating dynamically with javascript. How can I do that on creation ?

var input = document.createElement('input');
input.className = 'my-input-class';
input.placeholder ???
dimitris93
  • 4,155
  • 11
  • 50
  • 86

3 Answers3

1
var Yourinput = document.createElement('input');
Yourinput.className = 'my-input-class';
Yourinput.placeholder = "Some text";

You can set it this way.

halfer
  • 19,824
  • 17
  • 99
  • 186
Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42
0

You can use the setAttribute method on the input:

input.setAttribute('placeholder', 'Some Awesome Text');

or, since .placeholder has a getter and a setter on it, you can do this:

input.placeholder = 'Some Awesome Text';
KevBot
  • 17,900
  • 5
  • 50
  • 68
-1

input.placeholder = "your_value" works for standard attributes otherwise use setAttribute.

See here

Community
  • 1
  • 1
arc.slate .0
  • 428
  • 3
  • 8