user5027402
user5027402

Reputation:

Adding both textbox and textarea dynamically

Need to add textbox and textarea dynamically to one of my forms. I found this example which works fine for adding textbox 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 textarea

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

to the above javascript

and to the 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 erroneous. How should I add a textarea along with the textbox?

Error

The maximum limit allowed is 10. Say I add 6 of these fields and then decide to use 5 of them. If, I remove the last (6th one in this case) all of them get removed.

EDIT

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

** I do not know much about javascripts.**

Upvotes: 4

Views: 2912

Answers (2)

Disha V.
Disha V.

Reputation: 1864

I think you are doing like this

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

And for this following should work

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

Upvotes: 3

Sumukh Barve
Sumukh Barve

Reputation: 1444

The input and textarea don't have the same parent. That's what's causing the problem.

This should be the DOM structure:

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

Of course, the <br> is optional. You should probably use CSS for formatting instead.

Here's how you add an input-textarea pair:

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

Here's a working example, which you can copy-paste:

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

Hope this helps.

PS: I've tried to make minimal changes to your code. There are other (better) ways of accomplishing what you want.


Edit 0: Improved formatting.

Upvotes: 2

Related Questions