0

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>
17
  • please, i submitted a question a bit similar to this but different approach in javascript. it's much more easy to study than the i han sent first,. Commented Jan 12, 2016 at 16:28
  • Possible duplicate of stackoverflow.com/questions/34746695/… Commented Jan 12, 2016 at 16:30
  • yeah, its a bit the same but the javascript is different, in new to javascript, so i prefer this one than the first one i posted because its is easier to study. Commented Jan 12, 2016 at 16:42
  • Solution is same as stackoverflow.com/a/34746914 Commented Jan 12, 2016 at 16:43
  • but how can i double the textbox? thats what my problem is, in .append() i can just create another one, but there i can't understand it. Commented Jan 12, 2016 at 16:49

2 Answers 2

3

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>

Sign up to request clarification or add additional context in comments.

2 Comments

i want to place all the values the textbox with the placeholder of 'text' be separated from the the values of the textboxes with placeholder 'textbox' and place it inside the textbox near the textbox where all values from the textboxes with the placeholder of textbox.
This is excellent and worked wonderfully - but when i add a label between the input text fields, the "remove feature only removes the "text" field instead of the entire "textbox" & "text" field
0

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>

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.