简体   繁体   中英

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>

Use like this....

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<div id="textboxes">
    Text box 1 <input type="text" value="10" />
</div>

<button onclick="AddNew()" >Add</button>
<script type="text/javascript">
    //Adding New Line Item Function
    var i = 1;
    function AddNew(){
    var div= document.getElementById('textboxes');  
        if (i <4){ 
            i++;            
           div.innerHTML = div.innerHTML+'<br>Text box '+i+' <input type="text" value="'+i*10+'" />';

        }
    }
</script>
</body>
</html>

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="-">';            
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM