Adding dynamic text and text field

I need to add a dynamic text box and text area to one of my forms. I found this example that works great for adding a text box dynamically.

Javascript

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div><input type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>

HTML

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
</div>

Result

enter image description here

I tried adding a text area

$(wrapper).append('<div><textarea name="desc[]"></textarea></div><a href="#" class="remove_field">Remove</a></div>');

to javascript above

and HTML

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]"></div>
    <div><textarea name="desc[]"></textarea></div>
</div>

but it turns out to be wrong. How to add a text box with a text box?

Mistake

The maximum allowable limit is 10. Let's say I add 6 of these fields, and then decided to use 5 of them. If, I delete the last (sixth in this case), they are all deleted.

EDIT

Link to the above code https://jsfiddle.net/x6krv00u/

** I don't know much about javascripts. **

+4
2

input textarea . .

DOM:

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div>
        <input type="text" name="mytext[]"><br>
        <textarea name="desc[]"></textarea>
    </div>
</div>

, <br> . , CSS .

input - textarea:

$(wrapper).append('<div>' +
    '<input name="mytext[]"><br>' +
    '<textarea name="desc[]"></textarea>' +
    '<a href="#" class="remove_field">Remove</a>' +
'</div>');

, -:

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div>
        <input type="text" name="mytext[]"><br>
        <textarea name="desc[]"></textarea>
    </div>
</div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script type="text/javascript">
$(document).ready(function() {
    var max_fields      = 10; //maximum input boxes allowed
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID

    var x = 1; //initlal text box count
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
        if(x < max_fields){ //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div>' +
                '<input name="mytext[]"><br>' +
                '<textarea name="desc[]"></textarea>' +
                '<a href="#" class="remove_field">Remove</a>' +
            '</div>');
    }
    });

    $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});
</script>

, .

PS: . () , .


0: .

+2

,

$(wrapper).append('<div><input type="text" name="mytext[]"/><textarea name="desc[]"></textarea></div><a href="#" class="remove_field">Remove</a></div>');

$(wrapper).on("click",".remove_field", function(e){ //user click on remove text
    $(this).parent().remove(); // you cannot pass 'div' in parent()
});
+3

Source: https://habr.com/ru/post/1614993/


All Articles