views:

1853

answers:

3

I have a webpage. There is a button called add. When this add button is clicked then 1 text box must be added. This should happen at client side only. I want to allow the user to add at most 10 text boxes.

How can I achieve it using javascript?

example:

  • only 1 text box is displayed
  • user click add >
  • 2 text boxes displayed
  • user clicks add >

I also wants to provide a button called "remove" by which the user can remove the extra text box

Can anyone provide me a javascript code for this??

+1  A: 

Untested, but this should work (assuming an element with the right id exists);

var add_input = function () {

    var count = 0;

    return function add_input() {
        count++;
        if (count >= 10) {
            return false;
        }
        var input = document.createElement('input');
        input.name = 'generated_input';
        document.getElementbyId('inputs_contained').appendChild(input);
    }

}();

add_input();
add_input();
add_input();
David Dorward
can u pleas giv the detail code along wit html.I am not familiar with javascript
+1  A: 

A solution using the jQuery framework:

<form>
<ul class="addedfields">
<li><input type="text" name="field[]" class="textbox" />
<input type="button" class="removebutton" value="remove"/></li>
</ul>
<input type="button" class="addbutton" value="add"/>
</form>

The jQuery script code:

$(function(){
  $(".addbutton").click(){
     if(".addedfields").length < 10){
       $(".addedfields").append(
         '<li><input type="text" name="field[]" class="textbox" />' + 
         '<input type="button" class="removebutton" value="remove"/></li>'
       );
     }
  }

  // live event will automatically be attached to every new remove button
  $(".removebutton").live("click",function(){
     $(this).parent().remove();
  });
});

Note: I did not test the code.

Edit: changed faulty quotation marks

Scharrels
The OP asked for javascript. Not a jQuery solution. Look at the tags next time for this information!
redsquare
Much as I dislike the prevalence of "Use jQuery!" as an answer for everything JS related on stackoverflow — jQuery *is* JavaScript.
David Dorward
A: 

I hope you are using jQuery.

<script src="jquery.js" type="text/javascript"></script> 
<script type="text/javascript"><!--


    $(document).ready(function(){

    var counter = 2;
    $("#add").click(function () {
    if(counter==11){
     alert("Too many boxes");
     return false;
    } 
     $("#textBoxes").html($("#textBoxes").html() + "<div id='d"+counter+"' ><label for='t2'> Textbox "+counter+"</label><input type='textbox' id='t"+counter+"' > </div>\n");
     ++counter;
    });

    $("#remove").click(function () {
    if(counter==1){
     alert("Can u see any boxes");
     return false;
    } 
     --counter;
     $("#d"+counter).remove();
    });
  });
// --></script>
</head><body>

 <div id='textBoxes'>
<div id='d1' ><label for="t1"> Textbox 1</label><input type='textbox' id='t1' ></div>
</div>
<input type='button' value='add' id='add'>
<input type='button' value='remove' id='remove'>
Ahmet Kakıcı