1

Hello guys I have this script below that is adding input, but I'm not able to insert the masks. The plugin I'm using https://github.com/igorescobar/jQuery-Mask-Plugin

In the first field the mask is running, when adding it is not adding the mask.

Could help?

<script type="text/javascript">
$(document).ready(function() {
    var wrapper         = $(".input_fields_wrap"); //Fields wrapper
    var add_button      = $(".add_field_button"); //Add button ID
    $(add_button).click(function(e){ //on add input button click
        e.preventDefault();
            $(wrapper).append("<div><input type=\"text\" name=\"mytext[]\" id=\"date\"/><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();
    })
});    
</script>

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]" id="date"></div>
</div>
<script type="text/javascript">
    $('#date').mask('00/00/0000');
</script>

3 Answers 3

1

First of all change id selector #date class .date because you can't have more than one element with the same id.

Second thing you need to move the $('.date').mask('00/00/0000'); inside the $(document).ready(function() {}) method.

Third thing, you have to bind mask to the newly created an input element. Below is the copy of your working code after the amending the above suggested changes.

    <script type="text/javascript">
    $(document).ready(function () {
        var wrapper = $(".input_fields_wrap"); //Fields wrapper
        var add_button = $(".add_field_button"); //Add button ID
        $(add_button).click(function (e) { //on add input button click
            e.preventDefault();
            $(wrapper).append("<div><input type=\"text\" name=\"mytext[]\" class=\"date\"/><a href=\"#\" class=\"remove_field\">Remove</a></div>");
            $('.date').mask('00/00/0000');
        });

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

<div class="input_fields_wrap">
    <button class="add_field_button">Add More Fields</button>
    <div><input type="text" name="mytext[]" class="date"></div>
</div>
<script type="text/javascript">
    $('#date').mask('00/00/0000');
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Anytime @Conseld
1

Add $('#date').mask('00/00/0000'); inside $(document).ready(function() {}) As it is not guaranteed document has finished loading when you run it in a script block like this. Doco: https://igorescobar.github.io/jQuery-Mask-Plugin/docs.html

Comments

0

Slightly cleaned up.

var wrapper = $(".input_fields_wrap");
var button = $(".add_field_button");
var input = $("#date");

button.click(function (e) {
    e.preventDefault();
    wrapper.append("<div><input type=\"text\" name=\"mytext[]\" id=\"date\"/><a href=\"#\" class=\"remove_field\">Remove</a></div>");
});

wrapper.on("click", ".remove_field", function (e) {
    e.preventDefault();
    $(this).parent('div').remove();
})

input.mask('00/00/0000');

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.