0

I'm trying to make my textfields stack in descending order when adding a new textfield.

Here is my code:

JS:

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" class="form-control" name="mytext[]" placeholder="<Menu Name / Transaction ID/ Description>"/> <select><option ></option><option >Add</option></select><a href="#" class="remove_field"> &nbsp; Delete</a></div>'); //add input box

    }
});

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

I tried using the js sort method but that didn't work....

Thanks for help in advance!

2
  • What do you mean didn't work? Commented Jul 31, 2015 at 14:48
  • What does not work mean? Any errors? Where is the HTML that goes with this? Commented Jul 31, 2015 at 14:57

1 Answer 1

2

You probably messed up with id and class, as .XXX mean an element with class XXX, while an element with id YYY should use a selector #YYY.

You can either change .add_field_button to #add_field_button or change the button with id add_field_button to have a class add_field_button as what I've done in snippet.

And if you want descending, use .prepend() so the latest added textbox will insert to the front of the container.

$(function() {
var max_fields      = 10; //maximum input boxes allowed
var wrapper         = $(".input_fields_wrap"); //Fields wrapper
var add_button      = $(".add_field_button"); //Add button CLASS

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).prepend('<div><input type="text" class="form-control" name="mytext[]" placeholder="<Menu Name / Transaction ID/ Description>"/> <select><option ></option><option >Add</option></select><a href="#" class="remove_field"> &nbsp; Delete</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 src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<button class="add_field_button">Test</button>
<div class="input_fields_wrap"></div>

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

2 Comments

Thanks!! Thats exactly what I'm looking for!
That makes the code on stack snippet with jquery work, without that, I have to rewrite all of your work to pure javascript.

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.