Ratana Ratana - 1 year ago 168
HTML Question

HTML Add value in textbox

I want to add value in text box. A text box is already created with default value equal 10, then a button use a function to adding
new text box with limit 4. therefore, i want to add value continuously
from text box 1 to text box 4


Text box 1 value = 10

Text box 2 value = 20

Text box 3 value = 30

Text box 4 value = 40

How can i do this?

<script type="text/javascript">
//Adding New Line Item Function
var i = 1;
function AddNew(){
if (i <=3){ //if you don't want limit, you remove IF condition
var div= document.createElement('div');
div.innerHTML = '<input type="text" name="lineitem_'+i+'" maxlength="2" size="2"><input type="button" onclick="removeKid(this)" value="-">';

Answer Source

You want to add value continuously from text box 1 to text box 4 ,so while loop is more better here

 var i = 1;
    function AddNew(){
        while(i < 4 ){
            var div = document.getElementById("addingitem");
            div.innerHTML += '<br>Text box '+i+' <input type="text" name="lineitem_'+i+'" maxlength="2" size="2" value="'+i*10+'"><input type="button" onclick="removeKid(this)" value="-">';            
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download