Ratana Ratana - 6 months ago 20
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

Example:

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
i++;
var div= document.createElement('div');
div.innerHTML = '<input type="text" name="lineitem_'+i+'" maxlength="2" size="2"><input type="button" onclick="removeKid(this)" value="-">';
document.getElementById('addingitem').appendChild(div);
}
}
</script>

Answer

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 ){
            i++;
            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="-">';            
        }
    }