How to change this input value when a button is clicked?

I am trying to write a program with several text fields and 1 button. When the user clicks the button, the value for the text fields changes to how are you.

I'm not sure how to refer to a value ifor an expressiondocument.getElementById('text'+i).value= 'how are you'

 <input name="text1" type="text" id="text1" value="textbox 1"                 
 onFocus="this.style.background ='yellow'" onBlur="this.style.background='white'">
 <input name="text2" type="text" id="text2" value="textbox 2"                             
 onFocus="this.style.background = 'yellow'" onBlur="this.style.background='white'">
function popup() {
    for( int i; i <2, i++) {      
      document.getElementById('text'+i).value= 'how are you'      
    }
}  
<input type="button" value="Click Me!" onclick="popup()"><br />

I changed part of the for loop, but still not quite working:

function popup() {
    for( var i = 1, i <= 2, i++) {
      document.getElementById('text'+i).value= 'how are you'
    }
}

Will the program work with the foreach loop, as in C ++? First, it will count the number of text fields that you have, and then create a list that goes from 1 to the total number of text fields, then write a for loop to count the text fields.

+3
source share
4 answers
for(var i = 1; i <=2; i++) {
    document.getElementById('text'+i).value= 'how are you'      
}

Explanations:

  • You need init to ibe1
  • i<=2

JavaScript - , .

, @esailija:

  • i .
  • , ;
  • int i var i
  • .
+4

var JS. .

function popup() 
{  
  for(var i=1; i <= 2; i++) {     
    document.getElementById('text'+i).value = 'how are you';        
  }  
} 
+1

, for, , . :

var str = 'how are you';
document.getElementById('text1').value = str;   
document.getElementById('text2').value = str;   
+1
  • var javascript
  • for-loop
  • for-loop , , . < 2 1.

, , . + i . .

"text1" "text2"

for( var i=1; i <= 2; i++) {
  document.getElementById('text'+i).value = 'how are you' + i;
}
0
source

All Articles