1

I have a web page. There is a button called add. When this add button is clicked then one 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 one text box is displayed
  • user click add >
  • Two 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?

4
  • 3
    do you use jquery or some other javascript framework ? Commented Aug 9, 2009 at 10:17
  • What do you mean by textbox, do you mean asp.net textbox control? Commented Aug 9, 2009 at 10:31
  • 2
    @Amr - The OP states clientside. How does that mean an asp.net textbox?? Commented Aug 9, 2009 at 10:33
  • Sounds a lot like homework... Commented Aug 9, 2009 at 10:33

3 Answers 3

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

can u pleas giv the detail code along wit html.I am not familiar with javascript
1

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

2 Comments

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

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'>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.