Using For Loop in Javascript? -


also need loop add more names when put number 2 instead of 1. having issues code appreciated. know simple loop coding, stumped.

html

<p>enter first name: <input type="text" id="firstname">  <span id="firstname_error"></span> </p> <p>enter last name: <input type="text" id="lastname">   <span id="lastname_error"></span> </p> <p>how many pets have?  (0-3):   <input type="text" id="numpets" size="1" maxlength="1">   <span id="numpets_error"></span> </p> <p>list pet's names:     <input type="text" id="pet1">     <input type="text" id="pet2">     <input type="text" id="pet3"> </p>  <p><input id="mybutton" type="button" value="submit information"></p>  <p id="message"></p> 

javascript

for(counter=1; counter<=numpets; counter++) {   var petid = "pet" + counter;   var mypetname = document.getelementbyid(petid).value;   // code append test message variable } 

i have total of 3 fields in var "numpets", if put number 1 in field, read name in number 1 field. if put 2 read name in number 2 field. need able read 3 three fields.

the loop ok. have put loop inside function , call button. , define numpets did var namepet.


Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -