Good day to all, the question is:

I have in the html file a set of input and span values. Is it possible to create a function that, when the element id passed there, will output the value contained in input or span and change it?

Tried it but it doesn't work, the question is why?

 function st(x) { var y = document.getElementById(x); y.innerHTML = "99"; } 
 <input id=something onchange="st(this.id);"> 

Maybe the question is stupid, do not judge strictly, I am a beginner, in advance HUGE THANKS. ))

  • for an input element, the value is set by assigning the value property: y.value = "99"; - Igor
  • If you look at the DOM, you can make sure that everything works. 99 in place. - vp_arth
  • Igor thank you very much, the error was very stupid, now everything works, thanks) - user239808
  • Um, how now to remove this shameful question? - user239808

1 answer 1

The field inside input is specified by the value attribute:

 <input value="...value..."> 

 function st(x) { var y = document.getElementById(x); y.value = "99"; } 
 <input id=something onchange="st(this.id);"> 

Using innerHTML you can set the text inside the tag, between the closing and opening tag:

 <div>...innerHTML...</div> <span>...innerHTML...</span> 

 function st(x) { var y = document.getElementById(x); y.innerHTML = "99"; } 
 <div id=something onClick="st(this.id);">Нажмите здесь</div>