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
  • 3,977
  • 10
  • 46
  • 82

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,471
  • 17
  • 87
  • 173
Sumanth Jois
  • 3,054
  • 2
  • 25
  • 38
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
  • 16,116
  • 5
  • 48
  • 66
-1

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

See here

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