0
var 
nInput = document.createElement('input'),

clsinput = document.getElementsByTagName('input');

clsinput.setAttribute('class','new');
River
  • 8,585
  • 14
  • 54
  • 67
ahmed
  • 19
  • 5
  • getElementsByClassName returns a collection of element, do you want the first? – Axnyff Jan 01 '18 at 19:33
  • you can use nInput directly. `nInput.setAttribute('class', 'new').` document.getElementsByTagName returns an array – SunriseM Jan 01 '18 at 19:35

1 Answers1

0
var nInput = document.createElement('input'),

clsinput = document.getElementsByTagName('input');

clsinput.setAttribute('class','new');

getElementsByTagName returns an array. You are trying to set an attribute to that returned array. You have to iterate that array and set attribute.

Oh wait, you don't need that middle line at all. You created an input element in first line, just use that and set attribute to it

var nInput = document.createElement('input'),
nInput.setAttribute('class','new');
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307