Dynamically add and remove TextBoxes and get value of dynamic TextBox using jQuery

I have a problem. The code below is for adding and removing textbox dymically through .append() and .remove(). I want all the data inside the textboxes with a placeholder of textbox will be imploded and will be placed in the textbox I set with name of textbox. Also the ones with the placeholder of box will be placed in the allotted textbox for it. How to do that?

Here's my code:

HTML

<form class="form-horizontal" method= "Post">
    <div class="control-group">
        <div class="inc">
            <div class="controls">
                <input type="text" class="form-control" name="textbox" placeholder="textbox"/> 
                <input type="text" class="form-control" name="text" placeholder="text"/>
                <button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
                Add Textbox</button>
                <br>
                <br>
            </div>
        </div>
        <input type="submit" class="btn btn-info" name="submit" value="submit"/> 
        <input type="text" class="form-control" name="textbox" placeholder="texbox"/> 
    <input type="text" class="form-control" name="text" placeholder="text"/>
    </div>
</form>

JavasScript:

<script type="text/javascript">
    jQuery(document).ready( function () {
        $("#append").click( function() {
        $(".inc").append('
            <div class="controls">
                <input class="form-control" type="text" name="textbox" placeholder="textbox">
                <input class="form-control" type="text" name="text" placeholder="text">
                <a href="#" class="remove_this btn btn-danger">remove</a>
                <br>
                <br>
            </div>');
        return false;
        });

    jQuery('.remove_this').live('click', function() {
        jQuery(this).parent().remove();
        return false;
        });
    });
</script>

Upvotes: 0

Views: 23969

Answers (2)

amrinder singh
amrinder singh

Reputation: 1

jQuery(document).ready( function () {
        $("#append").click( function(e) {
          e.preventDefault();
        $(".inc").append('<div class="controls">\
                <input class="form-control" type="text" name="textbox" placeholder="textbox">\
                <input class="form-control" type="text" name="text" placeholder="text">\
                <a href="#" class="remove_this btn btn-danger">remove</a>\
                <br>\
                <br>\
            </div>');
        return false;
        });

    jQuery(document).on('click', '.remove_this', function() {
        jQuery(this).parent().remove();
        return false;
        });
    $("input[type=submit]").click(function(e) {
      e.preventDefault();
      $(this).next("[name=textbox]")
      .val(
        $.map($(".inc :text"), function(el) {
          return el.value
        }).join(",\n")
      )
    })
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<form class="form-horizontal" method= "Post">
    <div class="control-group">
        <div class="inc">
            <div class="controls">
                <input type="text" class="form-control" name="textbox" placeholder="textbox"/> 
                <input type="text" class="form-control" name="text" placeholder="text"/>
                <button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
                Add Textbox</button>
                <br>
                <br>
            </div>
        </div>
        <input type="submit" class="btn btn-info" name="submit" value="submit"/> 
        <input type="text" class="form-control" name="textbox" placeholder="texbox"/> 
    <input type="text" class="form-control" name="text" placeholder="text"/>
    </div>
</form>

Upvotes: 0

guest271314
guest271314

Reputation: 1

jQuery(document).ready( function () {
        $("#append").click( function(e) {
          e.preventDefault();
        $(".inc").append('<div class="controls">\
                <input class="form-control" type="text" name="textbox" placeholder="textbox">\
                <input class="form-control" type="text" name="text" placeholder="text">\
                <a href="#" class="remove_this btn btn-danger">remove</a>\
                <br>\
                <br>\
            </div>');
        return false;
        });

    jQuery(document).on('click', '.remove_this', function() {
        jQuery(this).parent().remove();
        return false;
        });
    $("input[type=submit]").click(function(e) {
      e.preventDefault();
      $(this).next("[name=textbox]")
      .val(
        $.map($(".inc :text"), function(el) {
          return el.value
        }).join(",\n")
      )
    })
  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
<form class="form-horizontal" method= "Post">
    <div class="control-group">
        <div class="inc">
            <div class="controls">
                <input type="text" class="form-control" name="textbox" placeholder="textbox"/> 
                <input type="text" class="form-control" name="text" placeholder="text"/>
                <button style="margin-left: 50px" class="btn btn-info" type="submit" id="append" name="append">
                Add Textbox</button>
                <br>
                <br>
            </div>
        </div>
        <input type="submit" class="btn btn-info" name="submit" value="submit"/> 
        <input type="text" class="form-control" name="textbox" placeholder="texbox"/> 
    <input type="text" class="form-control" name="text" placeholder="text"/>
    </div>
</form>

Upvotes: 3

Related Questions